Esempio n. 1
0
        private void RefactorFieldNamesInFieldReferences(CodeExpression expression, string newName)
        {
            // Do we have a field reference in the expression we are given?
            if (typeof(CodeFieldReferenceExpression) != expression.GetType())
            {
                return;
            }

            CodeFieldReferenceExpression frefStatement = (CodeFieldReferenceExpression)expression;

            // Is target object of field reference referring to a variable declaration?
            if (typeof(CodeVariableReferenceExpression) == frefStatement.TargetObject.GetType())
            {
                CodeVariableReferenceExpression vrefStatement = (CodeVariableReferenceExpression)frefStatement.TargetObject;
                // Get a reference to the variable declaration statement.
                CodeVariableDeclarationStatement vdeclStatement = stackVariables[vrefStatement.VariableName];

                // Is this a variable of type we are modifying now?
                if (vdeclStatement.Type.BaseType == newName)
                {
                    // Then we can convert the field names to PascalCase.
                    frefStatement.FieldName = PascalCaseConverterHelper.GetPascalCaseName(frefStatement.FieldName);
                }
            }
        }
Esempio n. 2
0
        protected override void OnTypeNameChanged(CodeTypeExtension typeExtension, string oldName, string newName)
        {
            // Prepare the MessageContractAttribute attribute to specify the type name on the wire.
            CodeAttributeDeclaration messageContractAttribute = typeExtension.FindAttribute("System.ServiceModel.MessageContractAttribute");

            if (messageContractAttribute != null)
            {
                CodeAttributeArgument wrapperNameArgument = messageContractAttribute.Arguments
                                                            .OfType <CodeAttributeArgument>()
                                                            .FirstOrDefault(arg => arg.Name.Equals("WrapperName", StringComparison.OrdinalIgnoreCase));

                if (wrapperNameArgument == null)
                {
                    return;
                }

                CodePrimitiveExpression wrapperNameValue = wrapperNameArgument.Value as CodePrimitiveExpression;
                if (wrapperNameValue != null && !string.IsNullOrEmpty((string)wrapperNameValue.Value))
                {
                    string newWrapperNameValue = PascalCaseConverterHelper.GetPascalCaseName((string)wrapperNameValue.Value);
                    wrapperNameArgument.Value = new CodePrimitiveExpression(newWrapperNameValue);
                }
            }
            else
            {
                CodeAttributeDeclaration xmlType =
                    new CodeAttributeDeclaration("System.ServiceModel.MessageContractAttribute",
                                                 new CodeAttributeArgumentExtended("WrapperName",
                                                                                   new CodePrimitiveExpression(oldName), true));

                // Add the XmlTypeAttribute attribute to ctd.
                typeExtension.AddAttribute(xmlType);
            }
        }
Esempio n. 3
0
        private void ConvertEnumMemberName(CodeTypeMemberExtension typeMemberExtension)
        {
            // Get a copy of the original type name.
            string oldName = typeMemberExtension.ExtendedObject.Name;
            // Change the field/property name to Pascal case.
            string newName = PascalCaseConverterHelper.GetPascalCaseName(oldName);

            typeMemberExtension.ExtendedObject.Name = newName;
            OnEnumMemberChanged(typeMemberExtension, oldName, newName);
        }
Esempio n. 4
0
        /// <summary>
        /// Changes the given type's name to Pascal case.
        /// </summary>
        private string ConvertTypeName(out string oldName)
        {
            // Holds the original type name.
            oldName = this.typeExtension.ExtendedObject.Name;
            // Change the type name to Pascal case.
            string newName = PascalCaseConverterHelper.GetPascalCaseName(oldName);

            this.typeExtension.ExtendedObject.Name = newName;
            OnTypeNameChanged(this.typeExtension, oldName, newName);
            return(newName);
        }
Esempio n. 5
0
        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);
        }