我實際上決定做的是在代碼文件生成後,通過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