Thinktecture.Tools.Web.Services.CodeGeneration.DataContractConverter.OnTypeNameChanged C# (CSharp) Method

OnTypeNameChanged() protected method

protected OnTypeNameChanged ( CodeTypeExtension typeExtension, string oldName, string newName ) : void
typeExtension CodeTypeExtension
oldName string
newName string
return void
        protected override void OnTypeNameChanged(CodeTypeExtension typeExtension, string oldName, string newName)
        {
            // Preserve Name values of XmlTypeAttribute or XmlRootAttribute here, because
            // the XML names can be different than the .NET class name. This occurs, for example,
            // when two XSD types have the same localname, but different XML namespaces.
            // The code generator then renames the second .NET class, while the names in XML
            // attributes should not be changed.
            // See also: http://wscfblue.codeplex.com/workitem/12733.

            // If [XmlTypeAttribute(TypeName="XXX")] already exists, preserve the XXX value.
            CodeAttributeDeclaration xmlType =
                typeExtension.FindAttribute("System.Xml.Serialization.XmlTypeAttribute");
            if (xmlType != null)
            {
                CodeAttributeArgument typeName = xmlType.FindArgument("TypeName");
                if (typeName != null)
                {
                    CodePrimitiveExpression expr = (CodePrimitiveExpression)typeName.Value;
                    oldName = expr.Value.ToString();
                }
            }

            // Prepare the XmlTypeAttribute attribute to specify the type name on the wire.
            xmlType =
                new CodeAttributeDeclaration("System.Xml.Serialization.XmlTypeAttribute",
                new CodeAttributeArgumentExtended("TypeName",
                new CodePrimitiveExpression(oldName), true));

            // Add/merge the XmlTypeAttribute attribute to ctd.
            typeExtension.AddAttribute(xmlType);

            // Prepare the XmlRootAttribute attribute to specify the type name on the wire.
            CodeAttributeDeclaration xmlRoot =
                new CodeAttributeDeclaration("System.Xml.Serialization.XmlRootAttribute",
                new CodeAttributeArgumentExtended("ElementName",
                new CodePrimitiveExpression(oldName), true));

            // Add/merge XmlRootAttribute attribute to ctd.
            typeExtension.AddAttribute(xmlRoot);
        }