user3215952 user3215952 - 2 months ago 9
C# Question

T4Toolbox remove GeneratedCodeAttribute

I've a problem with:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34230")]


With windows 8 and windows 7, we have diffrent versions of this number (4.0.30319.34230), when we generate the files. So even when I generate the new stuff, I have changes inside the files and the only thing which has changed was this number.

Is there a way to don't add this attribute to the generated code or a solution to have the same number for Windows 8 and Windows 7?

Answer

I solved it with the following code in the tt file:

...
<#@ 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);
    }
}
#>

NRefacotry on nuget

The result is:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0")]
Comments