我实际上决定做的是在代码文件生成后,通过Python脚本添加#if/#endif
标签。罗伯特的答复在功能上是有效的,但我只是觉得不对,只有一个应该就好了。
虽然它在数据模型生成中引入了另一种语言,但这看起来像是获得我想要的最干净的方法。我们正在使用的脚本如下。现在只需要检查NonSerializable属性(特别是PropertyChanged事件),因为我们采用了新的方式来构建数据合同。
#!/usr/bin/env python
import sys
from optparse import OptionParser
import shutil
# Use OptionParser to parse script arguments.
parser = OptionParser()
# Filename argument.
parser.add_option("-f", "--file", action="store", type="string", dest="filename", help="C# class file to parse", metavar="FILE.cs")
# Parse the arguments to the script.
(options, args) = parser.parse_args()
# The two files to be used: the original and a backup copy.
filename = options.filename
# Read the contents of the file.
f = open(filename, 'r')
csFile = f.read()
f.close()
# Add #if tags to the NonSerialized attributes.
csFile = csFile.replace(' [field: NonSerialized()]',
' #if !SILVERLIGHT\r\n [field: NonSerialized()]\r\n #endif')
# Rewrite the file contents.
f = open(filename, 'r')
f.write(csFile)
f.close()
不是一个答案,但如果你刚刚开始这个,你最好使用T4模板。 – Jeff
不幸的是,我们并不是真的“刚刚开始” - 当我们准备将数据模型分享给Silverlight方面时,这个问题就突然出现了。该代码是由我们不拥有的XSD文件生成的。我之前没有听说过T4模板,所以这可能是未来学习的东西。 :) – dythim