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

OnEnumMemberChanged() protected method

protected OnEnumMemberChanged ( CodeTypeMemberExtension memberExtension, string oldName, string newName ) : void
memberExtension CodeTypeMemberExtension
oldName string
newName string
return void
        protected override void OnEnumMemberChanged(CodeTypeMemberExtension memberExtension, string oldName, string newName)
        {
            // Fix references found in DefaultValue attributes.
            foreach (CodeTypeExtension type in Code.DataContracts)
            {
                foreach (CodeTypeMemberExtension member in type.Fields)
                {
                    CodeAttributeDeclaration attribute = member.FindAttribute("System.ComponentModel.DefaultValueAttribute");
                    if (attribute == null) continue;

                    CodeAttributeArgument argument = attribute.Arguments[0];
                    CodeFieldReferenceExpression argumentValue = argument.Value as CodeFieldReferenceExpression;
                    if (argumentValue == null) continue;

                    string baseTypeName = ((CodeTypeReferenceExpression)argumentValue.TargetObject).Type.BaseType;
                    string nameOfTypeInAttribute = PascalCaseConverterHelper.GetPascalCaseName(baseTypeName);
                    string nameOfTypeBeingChanged = memberExtension.Parent.ExtendedObject.Name;

                    if (argumentValue.FieldName == oldName && nameOfTypeInAttribute == nameOfTypeBeingChanged)
                    {
                        argumentValue.FieldName = newName;
                    }
                }

                // Fix references found in constructor where default values are set.
                // This is required for fixed references to enum values.
                // e.g. <xs:attribute ref="xlink:type" fixed="simple"/>
                foreach (CodeTypeMemberExtension ctorExtension in type.Constructors)
                {
                    // Get a reference to the actual constructor object.
                    CodeConstructor constructor = (CodeConstructor)ctorExtension.ExtendedObject;

                    // Do this for all statements we have in the constructor.
                    foreach (CodeStatement statement in constructor.Statements)
                    {
                        // Is this an assign statement?
                        CodeAssignStatement assignStatement = statement as CodeAssignStatement;
                        if (assignStatement != null)
                        {
                            // Do we have a field reference on the right side of the assignment statement?
                            CodeFieldReferenceExpression fieldRef = assignStatement.Right as CodeFieldReferenceExpression;
                            if (fieldRef != null)
                            {
                                // Does the referenced field belong to a type reference?
                                if (typeof(CodeTypeReferenceExpression) == fieldRef.TargetObject.GetType())
                                {
                                    string baseTypeName = ((CodeTypeReferenceExpression)fieldRef.TargetObject).Type.BaseType;
                                    string nameOfTypeForField = PascalCaseConverterHelper.GetPascalCaseName(baseTypeName);
                                    string nameOfTypeBeingChanged = memberExtension.Parent.ExtendedObject.Name;

                                    // Change the field name if it's changed.
                                    if (fieldRef.FieldName == oldName && nameOfTypeForField == nameOfTypeBeingChanged)
                                    {
                                        // Fix the field name first.
                                        fieldRef.FieldName = newName;

                                        // Also fix the name in the type reference.
                                        ((CodeTypeReferenceExpression)fieldRef.TargetObject).Type.BaseType = nameOfTypeForField;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Before adding the XmlEnumAttribute attribute to the CodeTypeMember
            // we have to make sure that the following attributes are not present.
            // If the 'XmlEnumAttribute' is already present the original value was not a valid name
            // and there should be no attempt to perform a rename.
            if (memberExtension.FindAttribute("System.Xml.Serialization.XmlAttributeAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyElementAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyAttributeAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlEnumAttribute") != null)
            {
                // We cannot proceed.
                return;
            }
            // Create a CodeAttributeDeclaration for XmlEnumAttribute attribute and
            // add it to the attributes collection.
            CodeAttributeDeclaration xmlEnum = new CodeAttributeDeclaration
                ("System.Xml.Serialization.XmlEnumAttribute");
            xmlEnum.Arguments.Add(new CodeAttributeArgumentExtended("Name",
                new CodePrimitiveExpression(oldName), true));

            // Finally add it to the custom attributes collection.
            memberExtension.AddAttribute(xmlEnum);
        }