我遇到的问题是:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34230")]
使用Windows 8和Windows 7,当我们生成文件时,我们有这个数字的不同版本(4.0.30319.34230)。因此,即使我生成新的东西,我在文件中也有变化,唯一改变的是这个数字。
有没有办法不将此属性添加到生成的代码中,或者为Windows 8和Windows 7提供相同编号的解决方案?
答案 0 :(得分:0)
我用tt文件中的以下代码解决了它:
...
<#@ assembly name="$(SolutionDir)../res/ICSharpCode.NRefactory.dll" #>
<#@ assembly name="$(SolutionDir)../res/ICSharpCode.NRefactory.CSharp.dll" #>
<#@ import namespace="ICSharpCode.NRefactory.CSharp" #>
<#@ import namespace="System.CodeDom.Compiler" #>
...
<#+
public class XsdSchemaTransformer : CSharpTemplate
{
public void TransformSchemas(TransformationContext transformationContext)
{
...
foreach (string file in files)
{
...
string transformText = typesTemplate.Transform();
transformText = ModifyGeneratedCodeAttributeXmlVersionNumber(transformText);
typesTemplate.Context.Write(typesTemplate.Output, transformText);
}
}
private string ModifyGeneratedCodeAttributeXmlVersionNumber(string source)
{
CSharpParser parser = new CSharpParser();
SyntaxTree syntaxTree = parser.Parse(source);
IEnumerable<TypeDeclaration> typeDeclarations = syntaxTree.Descendants.OfType<TypeDeclaration>();
foreach (TypeDeclaration typeDeclaration in typeDeclarations)
{
AttributeSection generatedCodeAttributeSection = typeDeclaration.Attributes.FirstOrDefault(x => x.Attributes.FirstOrDefault(y => IsTypeOfGeneratedCodeAttribute(y.Type)) != null);
if (generatedCodeAttributeSection != null)
{
ICSharpCode.NRefactory.CSharp.Attribute generatedCodeAttribute = generatedCodeAttributeSection.Attributes.FirstOrDefault(x => IsTypeOfGeneratedCodeAttribute(x.Type));
PrimitiveExpression primitiveExpression = (PrimitiveExpression)generatedCodeAttribute.Arguments.ElementAt(1);
string xmlVersionNumber = Convert.ToString(primitiveExpression.Value);
string[] splittedVersionNumber = xmlVersionNumber.Split('.');
string formatedXmlVersionNumber = string.Format("{0}.{1}", splittedVersionNumber[0], splittedVersionNumber[1]);
generatedCodeAttribute.Arguments.Remove(primitiveExpression);
generatedCodeAttribute.Arguments.Add(new PrimitiveExpression(formatedXmlVersionNumber));
}
}
return syntaxTree.GetText();
}
private bool IsTypeOfGeneratedCodeAttribute(AstType type)
{
return type.ToString().Equals(typeof(GeneratedCodeAttribute).FullName);
}
}
#>
结果是:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0")]