Inheritance: AttributableCodeDomObject
 /// <summary>
 /// Creates a new instance of CodeTypeMemberExtension class.
 /// </summary>
 /// <param name="extendObject">An object to be decorated by this instance.</param>
 public CodeTypeMemberExtension(CodeTypeMember extendedObject, CodeTypeExtension parent) 
     : base(extendedObject)
 {                                  
     if (typeof(CodeMemberField) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Field;
     }
     else if (typeof(CodeMemberMethod) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Method;
     }
     else if (typeof(CodeMemberProperty) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Property;
     }
     else if (typeof(CodeMemberEvent) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Event;
     }
     else if (typeof(CodeSnippetTypeMember) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Snippet;
     }
     else if (typeof(CodeConstructor) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.Constructor;
     }
     else if (typeof(CodeTypeConstructor) == extendedObject.GetType())
     {
         this.kind = CodeTypeMemberKind.StaticConstructor;
     }
     this.parent = parent;
 }
        protected override void OnTypeNameChanged(CodeTypeExtension typeExtension, string oldName, string newName)
        {
            // Prepare the MessageContractAttribute attribute to specify the type name on the wire.
            CodeAttributeDeclaration xmlType =
                new CodeAttributeDeclaration("System.ServiceModel.MessageContractAttribute",
                new CodeAttributeArgumentExtended("WrapperName",
                new CodePrimitiveExpression(oldName), true));

            // Add the XmlTypeAttribute attribute to ctd.
            typeExtension.AddAttribute(xmlType);
        }
Beispiel #3
0
        public void Refactor(CodeTypeExtension typeExtension, string oldName, string newName)
        {
            RefactorType(typeExtension, oldName, newName);
            RefactorFields(typeExtension.Fields, oldName, newName);
            RefactorProperties(typeExtension.Properties, oldName, newName);

            // We refactor methods only for client types, service contracts and
            // service types.
            if (typeExtension.Kind == CodeTypeKind.ClientType ||
                typeExtension.Kind == CodeTypeKind.ServiceContract ||
                typeExtension.Kind == CodeTypeKind.ServiceType)
            {
                RefactorMethods(typeExtension.Methods, oldName, newName);
            }
        }
 public static PascalCaseConverterBase GetPascalCaseConverter(CodeTypeExtension typeExtension, ExtendedCodeDomTree code)
 {
     switch (typeExtension.Kind)
     {
         case CodeTypeKind.DataContract:
             return new DataContractConverter(typeExtension, code);
         case CodeTypeKind.MessageContract:
             return new MessageContractConverter(typeExtension, code);
         case CodeTypeKind.ServiceContract:
             return new ServiceContractConverter(typeExtension, code);
         case CodeTypeKind.ClientType:
             return new ClientTypeConverter(typeExtension, code);
         default:
             return null;
     }
 }
Beispiel #5
0
        public bool IsMatching(CodeTypeExtension type)
        {
            CodeTypeDeclaration ctd = (CodeTypeDeclaration)type.ExtendedObject;
            Debug.Assert(ctd != null, "Invalid type");

            if (ctd.BaseTypes.Count > 0)
            {
                foreach (CodeTypeReference ctr in ctd.BaseTypes)
                {
                    if (ctr.BaseType == "System.ServiceModel.ClientBase`1")
                    {
                        return true;
                    }
                }
            }

            return false;
        }
        protected override void OnTypeNameChanged(CodeTypeExtension typeExtension, string oldName, string newName)
        {
            // Prepare the XmlTypeAttribute attribute to specify the type name on the wire.
            CodeAttributeDeclaration xmlType =
                new CodeAttributeDeclaration("System.Xml.Serialization.XmlTypeAttribute",
                new CodeAttributeArgumentExtended("TypeName",
                new CodePrimitiveExpression(oldName), true));

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

            // Do we have an XmlRootAttribute attribute?
            CodeAttributeDeclaration xmlRoot = typeExtension.FindAttribute("System.Xml.Serialization.XmlRootAttribute");
            if (xmlRoot != null)
            {
                // Prepare the XmlRootAttribute attribute to specify the type name on the wire.
                xmlRoot = new CodeAttributeDeclaration("System.Xml.Serialization.XmlRootAttribute",
                    new CodeAttributeArgumentExtended("ElementName",
                    new CodePrimitiveExpression(oldName), true));

                // Add XmlRootAttribute attribute to ctd.
                typeExtension.AddAttribute(xmlRoot);
            }
        }
Beispiel #7
0
 public bool IsMatching(CodeTypeExtension type)
 {
     return (type.FindAttribute("System.ServiceModel.ServiceBehaviorAttribute") != null);
 }
Beispiel #8
0
 protected abstract void OnTypeNameChanged(CodeTypeExtension typeExtension, string oldName, string newName);
Beispiel #9
0
 protected abstract bool CanConvertTypeName(CodeTypeExtension typeExtension);
Beispiel #10
0
 protected override bool CanConvertTypeName(CodeTypeExtension typeExtension)
 {
     return true;
 }
Beispiel #11
0
 public bool IsMatching(CodeTypeExtension type)
 {
     return (type.FindAttribute("System.Xml.Serialization.XmlTypeAttribute") != null
         || type.FindAttribute("System.Xml.Serialization.XmlRootAttribute") != null);
 }
Beispiel #12
0
        /// <summary>
        /// This method contains the core implementation for generating the GeneratedCode
        /// instance.
        /// </summary>
        /// <remarks>
        /// This method decorates every type found in codeNamespace with a CodeTypeMemberExtension.
        /// And then it sends each type through series of ITypeFilters to figure out whether the type 
        /// is a service contract, service type, client type, message contract or data contract.
        /// </remarks>
        private void ParseAndFilterCodeNamespace()
        {
            ITypeFilter dataContractTypeFilter = new DataContractTypeFilter();
            ITypeFilter messageContractTypeFilter = new MessageContractTypeFilter();
            ITypeFilter serviceContractTypeFilter = new ServiceContractTypeFilter();
            ITypeFilter clientTypeTypeFilter = new ClientTypeTypeFilter();
            ITypeFilter serviceTypeTypeFilter = new ServiceTypeTypeFilter();

            for (int i = 0; i < codeNamespace.Types.Count; i++)
            {
                // Take a reference to the current CodeTypeDeclaration.
                CodeTypeDeclaration ctd = codeNamespace.Types[i];
                // Create a new instance of CodeTypeMemberExtension to wrap
                // the current CodeTypeDeclaration.
                CodeTypeExtension typeExtension = new CodeTypeExtension(ctd);

                // Also wrap the inner CodeTypeMember(s)
                ExtendTypeMembers(typeExtension);

                // Here we execute the type filters in the highest to lowest probability order.
                if (dataContractTypeFilter.IsMatching(typeExtension))
                {
                    typeExtension.Kind = CodeTypeKind.DataContract;
                    DataContracts.Add(typeExtension);
                    continue;
                }
                if (messageContractTypeFilter.IsMatching(typeExtension))
                {
                    typeExtension.Kind = CodeTypeKind.MessageContract;
                    MessageContracts.Add(typeExtension);
                    continue;
                }
                if (serviceContractTypeFilter.IsMatching(typeExtension))
                {
                    typeExtension.Kind = CodeTypeKind.ServiceContract;
                    ServiceContracts.Add(typeExtension);
                    continue;
                }
                if (clientTypeTypeFilter.IsMatching(typeExtension))
                {
                    typeExtension.Kind = CodeTypeKind.ClientType;
                    ClientTypes.Add(typeExtension);
                    continue;
                }
                if (serviceTypeTypeFilter.IsMatching(typeExtension))
                {
                    typeExtension.Kind = CodeTypeKind.ServiceType;
                    ServiceTypes.Add(typeExtension);
                    continue;
                }
                UnfilteredTypes.Add(typeExtension);
            }
        }
Beispiel #13
0
        /// <summary>
        /// This methods adds CodeTypeMemberExtension to all CodeTypeMembers in a 
        /// given type.
        /// </summary>        
        private static void ExtendTypeMembers(CodeTypeExtension typeExtension)
        {
            CodeTypeDeclaration type = (CodeTypeDeclaration)typeExtension.ExtendedObject;

            for (int i = 0; i < type.Members.Count; i++)
            {
                CodeTypeMember member = type.Members[i];
                CodeTypeMemberExtension memberExtension = new CodeTypeMemberExtension(member, typeExtension);

                // Add the member to the correct filtered collection.
                if (memberExtension.Kind == CodeTypeMemberKind.Field)
                {
                    typeExtension.Fields.Add(memberExtension);
                }
                else if (memberExtension.Kind == CodeTypeMemberKind.Property)
                {
                    typeExtension.Properties.Add(memberExtension);
                }
                else if (memberExtension.Kind == CodeTypeMemberKind.Method)
                {
                    typeExtension.Methods.Add(memberExtension);
                }
                else if (memberExtension.Kind == CodeTypeMemberKind.Constructor ||
                    memberExtension.Kind == CodeTypeMemberKind.StaticConstructor)
                {
                    typeExtension.Constructors.Add(memberExtension);
                }
                else if (memberExtension.Kind == CodeTypeMemberKind.Event)
                {
                    typeExtension.Events.Add(memberExtension);
                }
                else
                {
                    typeExtension.Unknown.Add(memberExtension);
                }

                // Finally update the collection item reference.
                type.Members[i] = memberExtension;
            }
        }
Beispiel #14
0
 private void RefactorType(CodeTypeExtension typeExtension, string oldName, string newName)
 {
     CodeTypeDeclaration type = (CodeTypeDeclaration)typeExtension.ExtendedObject;
     RefactorCodeTypeReferenceCollection(type.BaseTypes, oldName, newName);
     RefactorCodeTypeReferencesInAttributes(type.CustomAttributes, oldName, newName);
 }
        private void CreateServiceType()
        {
            // We can create the service type(s) only if we have one or more service
            // contract.
            if (code.ServiceContracts.Count > 0)
            {
                // Take a reference to the first ServiceContract available.
                // IMPORTANT!:(Currently we only support single service type)
                // May be want to support multiple service contracts in the next version.
                CodeTypeExtension srvContract = code.ServiceContracts[0];
                // Notify if srvContract is null. This would mean that we have constructed a bad 
                // GeneratedCode instance from our CodeFactory.
                Debug.Assert(srvContract != null, "Generated service contract could not be null.");

                // Construct the service type name by removing the leading "I" character from 
				// the service contract name that was added for generation of the interface.
            	string srvTypeName = srvContract.ExtendedObject.Name.Substring(1);

                // Create a new instance of CodeTypeDeclaration type representing the service type.
                CodeTypeDeclaration srvType = new CodeTypeDeclaration(srvTypeName);

                // Also wrap the CodeTypeDeclaration in an extension.
                CodeTypeExtension typeExt = new CodeTypeExtension(srvType);

                // This class.
                srvType.IsClass = true;

				switch (options.MethodImplementation)
				{
					case MethodImplementation.PartialClassMethodCalls:
						// The service type is partial so that the implementation methods can be written in separate file.
						srvType.IsPartial = true;
						break;
					case MethodImplementation.AbstractMethods:
						// The service type is abstract so that the operation methods can be made abstract.
						srvType.TypeAttributes |= TypeAttributes.Abstract;
						break;
				}

                // And this implements the service contract interface.
				if (code.CodeLanguauge == CodeLanguage.VisualBasic)
				{
					srvType.Members.Add(new CodeSnippetTypeMember("Implements " + srvContract.ExtendedObject.Name));
				}
				else
				{
					srvType.BaseTypes.Add(new CodeTypeReference(srvContract.ExtendedObject.Name));					
				}

                // Now itterate the srvContractObject.Members and add each and every method in 
                // the service contract type to the new type being created.
                foreach (CodeTypeMemberExtension methodExtension in srvContract.Methods)
                {
                    // Get a referece to the actual CodeMemberMethod object extended
                    // by ext.
                    CodeMemberMethod method = methodExtension.ExtendedObject as CodeMemberMethod;

                    // Create a new CodeMemeberMethod and copy the attributes.
                    CodeMemberMethod newMethod = new CodeMemberMethod();
                    newMethod.Name = method.Name;

                    // Implemented method has to be public.
                    newMethod.Attributes = MemberAttributes.Public;

                    // Notify that this member is implementing a method in the service contract.
					if (code.CodeLanguauge == CodeLanguage.VisualBasic)
					{
						newMethod.ImplementationTypes.Add(new CodeTypeReference(srvContract.ExtendedObject.Name));
					}
					else
					{
						newMethod.ImplementationTypes.Add(srvType.BaseTypes[0]);						
					}

                    // Add all parametes to the newly created method.
                    foreach (CodeParameterDeclarationExpression cpde in method.Parameters)
                    {
                        newMethod.Parameters.Add(cpde);
                    }

                    // Set the return type.
                    newMethod.ReturnType = method.ReturnType;

					switch (options.MethodImplementation)
					{
						case MethodImplementation.PartialClassMethodCalls:
							{
								// Gather the parameters from the operation to pass into the implementation method.
								IEnumerable<CodeArgumentReferenceExpression> parameters = newMethod.Parameters
									.OfType<CodeParameterDeclarationExpression>()
									.Select(p => new CodeArgumentReferenceExpression(p.Name));

								// Create an expression to invoke the implementation method.
								CodeMethodInvokeExpression methodInvocation = new CodeMethodInvokeExpression(null, newMethod.Name + "Implementation", parameters.ToArray());

								// Check if the method has a return type.
								if (newMethod.ReturnType.BaseType != "System.Void")
								{
									// Make sure the call to the implementation method is returned.
									CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(methodInvocation);
									newMethod.Statements.Add(returnStatement);
								}
								else
								{
									// Add the call to the implementation method without a return.
									newMethod.Statements.Add(methodInvocation);
								}
							}
							break;
						case MethodImplementation.NotImplementedException:
							{
								// Create a new code statement to throw NotImplementedExcption.
								CodeThrowExceptionStatement niex = new CodeThrowExceptionStatement(
									new CodeObjectCreateExpression(
										new CodeTypeReference(typeof(NotImplementedException)), new CodeExpression[] { })
									);

								// Add it to the statements collection in the new method.
								newMethod.Statements.Add(niex);
							}
							break;
						case MethodImplementation.AbstractMethods:
							{
								// No statement is required for the abstract methods.
								newMethod.Attributes |= MemberAttributes.Abstract;
								break;
							}
					}

					// Wrap the CodeMemberMethod in an extension. This could be useful for other extensions.
					CodeTypeMemberExtension newMethodExt = new CodeTypeMemberExtension(newMethod, typeExt);
					srvType.Members.Add(newMethodExt);
                }

                // Add the ServiceBehaviorAttribute attribute.
            	CodeAttributeDeclaration serviceBehaviorAttribute = new CodeAttributeDeclaration(
            		new CodeTypeReference(typeof(ServiceBehaviorAttribute)));

				if (!string.IsNullOrEmpty(options.InstanceContextMode))
				{
					CodeTypeReferenceExpression instanceContextModeEnum = new CodeTypeReferenceExpression(typeof(InstanceContextMode));
					CodeFieldReferenceExpression instanceContextModeValue = new CodeFieldReferenceExpression(instanceContextModeEnum, options.InstanceContextMode);
					CodeAttributeArgument instanceContextModeArgument = new CodeAttributeArgument("InstanceContextMode", instanceContextModeValue);
					serviceBehaviorAttribute.Arguments.Add(instanceContextModeArgument);					
				}

				if (!string.IsNullOrEmpty(options.ConcurrencyMode))
				{
					CodeTypeReferenceExpression concurrencyModeEnum = new CodeTypeReferenceExpression(typeof(ConcurrencyMode));
					CodeFieldReferenceExpression concurrencyModeValue = new CodeFieldReferenceExpression(concurrencyModeEnum, options.ConcurrencyMode);
					CodeAttributeArgument concurrencyModeArgument = new CodeAttributeArgument("ConcurrencyMode", concurrencyModeValue);
					serviceBehaviorAttribute.Arguments.Add(concurrencyModeArgument);	
				}

				if (!options.UseSynchronizationContext)
				{
					CodeAttributeArgument useSynchronizationContextAttribute = new CodeAttributeArgument("UseSynchronizationContext", new CodePrimitiveExpression(false));
					serviceBehaviorAttribute.Arguments.Add(useSynchronizationContextAttribute);					
				}

            	typeExt.AddAttribute(serviceBehaviorAttribute);

                this.serviceTypeName = srvType.Name;
                // Finally add the newly created type to the code being generated.
                code.ServiceTypes.Add(typeExt);
            }
        }        
Beispiel #16
0
        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);
        }
Beispiel #17
0
 public bool IsMatching(CodeTypeExtension type)
 {
     return (type.FindAttribute("System.ServiceModel.MessageContractAttribute") != null);
 }
Beispiel #18
0
 public ClientTypeConverter(CodeTypeExtension typeExtension, ExtendedCodeDomTree code)
     : base(typeExtension, code)
 {
 }
Beispiel #19
0
 public ServiceContractConverter(CodeTypeExtension typeExtension, ExtendedCodeDomTree code)
     : base(typeExtension, code)
 {
 }
Beispiel #20
0
 protected PascalCaseConverterBase(CodeTypeExtension codeTypeExtension, ExtendedCodeDomTree code)
 {
     this.typeExtension = codeTypeExtension;
     this.Code = code;
     this.type = (CodeTypeDeclaration)codeTypeExtension.ExtendedObject;
 }
Beispiel #21
0
 protected override void OnTypeNameChanged(CodeTypeExtension typeExtension, string oldName, string newName)
 {
     // NOP
 }
Beispiel #22
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);
            }
        }