public void AddRange() { CodeAttributeDeclaration cad1 = new CodeAttributeDeclaration(); CodeAttributeDeclaration cad2 = new CodeAttributeDeclaration(); CodeAttributeDeclaration cad3 = new CodeAttributeDeclaration(); CodeAttributeDeclarationCollection coll1 = new CodeAttributeDeclarationCollection(); coll1.Add(cad1); coll1.Add(cad2); CodeAttributeDeclarationCollection coll2 = new CodeAttributeDeclarationCollection(); coll2.Add(cad3); coll2.AddRange(coll1); Assert.AreEqual(3, coll2.Count, "#1"); Assert.AreEqual(1, coll2.IndexOf(cad1), "#2"); Assert.AreEqual(2, coll2.IndexOf(cad2), "#3"); Assert.AreEqual(0, coll2.IndexOf(cad3), "#4"); CodeAttributeDeclarationCollection coll3 = new CodeAttributeDeclarationCollection(); coll3.Add(cad3); coll3.AddRange(new CodeAttributeDeclaration[] { cad1, cad2 }); Assert.AreEqual(3, coll2.Count, "#5"); Assert.AreEqual(1, coll2.IndexOf(cad1), "#6"); Assert.AreEqual(2, coll2.IndexOf(cad2), "#7"); Assert.AreEqual(0, coll2.IndexOf(cad3), "#8"); }
protected override void GenerateAttributeMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName) { CodeAttributeDeclaration att = new CodeAttributeDeclaration("System.Xml.Serialization.XmlAttribute"); if (forceUseMemberName || attinfo.Name != attinfo.AttributeName) { att.Arguments.Add(GetArg(attinfo.AttributeName)); } if (attinfo.Namespace != defaultNamespace) { att.Arguments.Add(GetArg("Namespace", attinfo.Namespace)); } if (attinfo.Form != XmlSchemaForm.None) { att.Arguments.Add(GetEnumArg("Form", "System.Xml.Schema.XmlSchemaForm", attinfo.Form.ToString())); } if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData)) { att.Arguments.Add(GetArg("DataType", attinfo.TypeData.XmlType)); } attributes.Add(att); if (attinfo.Ignore) { attributes.Add(new CodeAttributeDeclaration("System.Xml.Serialization.XmlIgnoreAttribute")); } }
internal void ExportRoot(StructMapping mapping, Type includeType) { if (!rootExported) { rootExported = true; ExportDerivedStructs(mapping); for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) { if (!derived.ReferencedByElement && derived.IncludeInSchema && !derived.IsAnonymousType) { CodeAttributeDeclaration include = new CodeAttributeDeclaration(includeType.FullName); include.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(derived.TypeDesc.FullName))); includeMetadata.Add(include); } } Hashtable typesIncluded = new Hashtable(); foreach (TypeMapping m in Scope.TypeMappings) { if (m is ArrayMapping) { ArrayMapping arrayMapping = (ArrayMapping)m; if (ShouldInclude(arrayMapping) && !typesIncluded.Contains(arrayMapping.TypeDesc.FullName)) { CodeAttributeDeclaration include = new CodeAttributeDeclaration(includeType.FullName); include.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(arrayMapping.TypeDesc.FullName))); includeMetadata.Add(include); typesIncluded.Add(arrayMapping.TypeDesc.FullName, string.Empty); EnsureTypesExported(arrayMapping.Elements, arrayMapping.Namespace); } } } } }
public ModelGenerate(CodeNamespaceImport[] lcnmSpace, string nmSpace, string className, CodeTypeReference[] lBaseClass, string generatePath) : base(lcnmSpace, nmSpace, className, lBaseClass, generatePath) { //CodeTypeReference ctr = new CodeTypeReference("Serializable"); CodeAttributeDeclaration cad = new CodeAttributeDeclaration("Serializable"); CodeAttributeDeclaration cad1 = new CodeAttributeDeclaration("DataContract"); CodeAttributeDeclarationCollection cadc = new CodeAttributeDeclarationCollection(); cadc.Add(cad); cadc.Add(cad1); this.MyClass.CustomAttributes = cadc; }
public void AddMappingMetadata(CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, string ns, bool forceUseMemberName) { CodeAttributeDeclaration att; TypeData memType = member.TypeMapMember.TypeData; if (member.Any) { XmlTypeMapElementInfoList list = (XmlTypeMapElementInfoList)((XmlTypeMapMemberElement)member.TypeMapMember).ElementInfo; foreach (XmlTypeMapElementInfo info in list) { if (info.IsTextElement) { metadata.Add(new CodeAttributeDeclaration("System.Xml.Serialization.XmlText")); } else { att = new CodeAttributeDeclaration("System.Xml.Serialization.XmlAnyElement"); if (!info.IsUnnamedAnyElement) { att.Arguments.Add(MapCodeGenerator.GetArg("Name", info.ElementName)); if (info.Namespace != ns) { att.Arguments.Add(MapCodeGenerator.GetArg("Namespace", member.Namespace)); } } metadata.Add(att); } } } else if (member.TypeMapMember is XmlTypeMapMemberList) { // Array parameter XmlTypeMapMemberList list = member.TypeMapMember as XmlTypeMapMemberList; ListMap listMap = (ListMap)list.ListTypeMapping.ObjectMap; codeGenerator.AddArrayAttributes(metadata, list, ns, forceUseMemberName); codeGenerator.AddArrayItemAttributes(metadata, listMap, memType.ListItemTypeData, list.Namespace, 0); } else if (member.TypeMapMember is XmlTypeMapMemberElement) { codeGenerator.AddElementMemberAttributes((XmlTypeMapMemberElement)member.TypeMapMember, ns, metadata, forceUseMemberName); } else if (member.TypeMapMember is XmlTypeMapMemberAttribute) { codeGenerator.AddAttributeMemberAttributes((XmlTypeMapMemberAttribute)member.TypeMapMember, ns, metadata, forceUseMemberName); } else { throw new NotSupportedException("Schema type not supported"); } }
protected override void GenerateElementMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member) { if (member.ChoiceMember != null) { CodeAttributeDeclaration att = new CodeAttributeDeclaration("System.Xml.Serialization.XmlChoiceIdentifier"); att.Arguments.Add(GetArg(member.ChoiceMember)); attributes.Add(att); } if (member.Ignore) { attributes.Add(new CodeAttributeDeclaration("System.Xml.Serialization.XmlIgnoreAttribute")); } }
public void CustomAttributes_SetNonNull_Get_ReturnsExpected() { CodeTypeMember member = new T() { CustomAttributes = null }; CodeAttributeDeclarationCollection value = new CodeAttributeDeclarationCollection(); value.Add(new CodeAttributeDeclaration("Name1")); value.Add(new CodeAttributeDeclaration("Name2")); member.CustomAttributes = value; Assert.Equal(value, member.CustomAttributes); }
public void CustomAttributes_SetNonNull_Get_ReturnsExpected() { var parameter = new CodeParameterDeclarationExpression() { CustomAttributes = null }; CodeAttributeDeclarationCollection value = new CodeAttributeDeclarationCollection(); value.Add(new CodeAttributeDeclaration("Name1")); value.Add(new CodeAttributeDeclaration("Name2")); parameter.CustomAttributes = value; Assert.Equal(value, parameter.CustomAttributes); }
internal void UCodeAddTestMethod(string methodName, Dictionary <string, string> methodAttribute, string reportCollection = "unifiedLogCollection", string reportInstance = "unifiedReport") { string testCaseDescription = string.Empty; CodeMemberMethod sampleMethod = new CodeMemberMethod(); sampleMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final; sampleMethod.Name = methodName; sampleMethod.ReturnType = new CodeTypeReference(typeof(void)); CodeAttributeDeclarationCollection collection = new CodeAttributeDeclarationCollection(); if (methodAttribute != null) { foreach (KeyValuePair <string, string> singleItem in methodAttribute) { if (singleItem.Key.ToLower().ToString().Contains("description")) { testCaseDescription = "\"" + singleItem.Value + "\""; } if (string.IsNullOrEmpty(singleItem.Value)) { collection.Add(new CodeAttributeDeclaration(singleItem.Key)); } else { collection.Add(new CodeAttributeDeclaration(singleItem.Key, new CodeAttributeArgument(new CodePrimitiveExpression(singleItem.Value)))); } } foreach (CodeAttributeDeclaration singleDeclaration in collection) { sampleMethod.CustomAttributes.Add(singleDeclaration); } } sampleMethod.Statements.Add(new CodeSnippetExpression(reportCollection + ".Add(" + reportInstance + ".StartTest(" + testCaseDescription + "));")); CodeTryCatchFinallyStatement testMethodTry = new CodeTryCatchFinallyStatement(); testMethodTry.TryStatements.Add(new CodeCommentStatement("Please fill in you code here")); sampleMethod.Statements.Add(testMethodTry); CodeCatchClause testMethodCatch = new CodeCatchClause("ex"); testMethodCatch.Statements.Add(new CodeCommentStatement("Code to handle the Exception")); testMethodTry.CatchClauses.Add(testMethodCatch); testMethodTry.FinallyStatements.Add(new CodeCommentStatement("Handle any finally block")); targetClass.Members.Add(sampleMethod); }
public void Add() { CodeAttributeDeclaration cad1 = new CodeAttributeDeclaration(); CodeAttributeDeclaration cad2 = new CodeAttributeDeclaration(); CodeAttributeDeclarationCollection coll = new CodeAttributeDeclarationCollection(); Assert.AreEqual(0, coll.Add(cad1), "#1"); Assert.AreEqual(1, coll.Count, "#2"); Assert.AreEqual(0, coll.IndexOf(cad1), "#3"); Assert.AreEqual(1, coll.Add(cad2), "#4"); Assert.AreEqual(2, coll.Count, "#5"); Assert.AreEqual(1, coll.IndexOf(cad2), "#6"); }
internal void AddTypeMetadata(CodeAttributeDeclarationCollection metadata, Type type, string defaultName, string name, string ns, bool includeInSchema) { CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(type.FullName); if (name == null || name.Length == 0) { attribute.Arguments.Add(new CodeAttributeArgument("AnonymousType", new CodePrimitiveExpression(true))); } else { if (defaultName != name) { attribute.Arguments.Add(new CodeAttributeArgument("TypeName", new CodePrimitiveExpression(name))); } } if (ns != null && ns.Length != 0) { attribute.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(ns))); } if (!includeInSchema) { attribute.Arguments.Add(new CodeAttributeArgument("IncludeInSchema", new CodePrimitiveExpression(false))); } if (attribute.Arguments.Count > 0) { metadata.Add(attribute); } }
public static void Add(this CodeAttributeDeclarationCollection collection, Type type, params string[] arguments) { var query = from item in arguments select new CodeAttributeArgument(new CodePrimitiveExpression(item)); collection.Add(new CodeAttributeDeclaration(new CodeTypeReference(type), query.ToArray())); }
private static void AppendMetadata(CodeAttributeDeclarationCollection from, CodeAttributeDeclarationCollection to) { foreach (CodeAttributeDeclaration declaration in from) { to.Add(declaration); } }
public void Constructor2() { CodeAttributeDeclaration cad1 = new CodeAttributeDeclaration(); CodeAttributeDeclaration cad2 = new CodeAttributeDeclaration(); CodeAttributeDeclarationCollection c = new CodeAttributeDeclarationCollection(); c.Add(cad1); c.Add(cad2); CodeAttributeDeclarationCollection coll = new CodeAttributeDeclarationCollection(c); Assert.AreEqual(2, coll.Count, "#1"); Assert.AreEqual(0, coll.IndexOf(cad1), "#2"); Assert.AreEqual(1, coll.IndexOf(cad2), "#3"); }
private void AddRootMetadata(CodeAttributeDeclarationCollection metadata, TypeMapping typeMapping, string name, string ns, ElementAccessor rootElement) { string fullName = typeof(XmlRootAttribute).FullName; foreach (CodeAttributeDeclaration declaration in metadata) { if (declaration.Name == fullName) { return; } } CodeAttributeDeclaration declaration2 = new CodeAttributeDeclaration(fullName); if (typeMapping.TypeDesc.Name != name) { declaration2.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(name))); } if (ns != null) { declaration2.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(ns))); } if ((typeMapping.TypeDesc != null) && typeMapping.TypeDesc.IsAmbiguousDataType) { declaration2.Arguments.Add(new CodeAttributeArgument("DataType", new CodePrimitiveExpression(typeMapping.TypeDesc.DataType.Name))); } if (rootElement.IsNullable) { declaration2.Arguments.Add(new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression(rootElement.IsNullable))); } metadata.Add(declaration2); }
protected override void GenerateElementInfoMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName) { CodeAttributeDeclaration att = new CodeAttributeDeclaration("System.Xml.Serialization.XmlElement"); if (forceUseMemberName || einfo.ElementName != member.Name) { att.Arguments.Add(GetArg(einfo.ElementName)); } if (einfo.TypeData.FullTypeName != defaultType.FullTypeName) { att.Arguments.Add(GetTypeArg("Type", einfo.TypeData.FullTypeName)); } if (einfo.Namespace != defaultNamespace) { att.Arguments.Add(GetArg("Namespace", einfo.Namespace)); } if (einfo.Form == XmlSchemaForm.Unqualified) { att.Arguments.Add(GetEnumArg("Form", "System.Xml.Schema.XmlSchemaForm", einfo.Form.ToString())); } if (einfo.IsNullable) { att.Arguments.Add(GetArg("IsNullable", true)); } if (!TypeTranslator.IsDefaultPrimitiveTpeData(einfo.TypeData)) { att.Arguments.Add(GetArg("DataType", einfo.TypeData.XmlType)); } if (addAlwaysAttr || att.Arguments.Count > 0) { attributes.Add(att); } }
protected override void GenerateArrayElement(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName) { XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo)member.ElementInfo[0]; CodeAttributeDeclaration att = new CodeAttributeDeclaration("System.Xml.Serialization.XmlArray"); if (forceUseMemberName || (einfo.ElementName != member.Name)) { att.Arguments.Add(GetArg("ElementName", einfo.ElementName)); } if (einfo.Namespace != defaultNamespace) { att.Arguments.Add(GetArg("Namespace", einfo.Namespace)); } if (einfo.Form == XmlSchemaForm.Unqualified) { att.Arguments.Add(MapCodeGenerator.GetEnumArg("Form", "System.Xml.Schema.XmlSchemaForm", einfo.Form.ToString())); } if (einfo.IsNullable) { att.Arguments.Add(GetArg("IsNullable", true)); } if (att.Arguments.Count > 0) { attributes.Add(att); } }
void GetProfileProviderAttribute(ProfileSection ps, CodeAttributeDeclarationCollection collection, string providerName) { if (String.IsNullOrEmpty(providerName)) { providerTypeName = FindProviderTypeName(ps, ps.DefaultProvider); } else { providerTypeName = FindProviderTypeName(ps, providerName); } if (providerTypeName == null) { throw new HttpException(String.Format("Profile provider type not defined: {0}", providerName)); } collection.Add( new CodeAttributeDeclaration( "ProfileProvider", new CodeAttributeArgument( new CodePrimitiveExpression(providerTypeName) ) ) ); }
void AddRootMetadata(CodeAttributeDeclarationCollection metadata, TypeMapping typeMapping, string name, string ns, ElementAccessor rootElement) { string rootAttrName = typeof(XmlRootAttribute).FullName; // check that we haven't already added a root attribute since we can only add one foreach (CodeAttributeDeclaration attr in metadata) { if (attr.Name == rootAttrName) { return; } } CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(rootAttrName); if (typeMapping.TypeDesc.Name != name) { attribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(name))); } if (ns != null) { attribute.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(ns))); } if (typeMapping.TypeDesc != null && typeMapping.TypeDesc.IsAmbiguousDataType) { attribute.Arguments.Add(new CodeAttributeArgument("DataType", new CodePrimitiveExpression(typeMapping.TypeDesc.DataType.Name))); } if ((object)(rootElement.IsNullable) != null) { attribute.Arguments.Add(new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression((bool)rootElement.IsNullable))); } metadata.Add(attribute); }
protected override void GenerateClassInclude(CodeAttributeDeclarationCollection attributes, XmlTypeMapping map) { CodeAttributeDeclaration iatt = new CodeAttributeDeclaration("System.Xml.Serialization.SoapInclude"); iatt.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(map.TypeData.FullTypeName))); attributes.Add(iatt); }
static void AppendMetadata(CodeAttributeDeclarationCollection from, CodeAttributeDeclarationCollection to) { foreach (CodeAttributeDeclaration attr in from) { to.Add(attr); } }
public override void CreateClasses() { try { CodeAttributeDeclarationCollection AttributeCollection = new CodeAttributeDeclarationCollection(); AttributeCollection.Add(new CodeAttributeDeclaration("Serializable")); CodeTypeDeclaration activityCls = new CodeTypeDeclaration { Name = "activity", IsClass = true, CustomAttributes = AttributeCollection }; CreateClassSummary(activityCls, string.Format("defines all the methods of {0} class", "Activity")); activityCls.Attributes = MemberAttributes.Assembly; activityCls.BaseTypes.Add(new CodeTypeReference { BaseType = "CActivity" }); AddMemberFields(ref activityCls); AddMemberFunctions(ref activityCls); base._csFile.UserDefinedTypes.Add(activityCls); } catch (Exception ex) { throw new Exception(string.Format("CreateClasses->{0}", !object.Equals(ex.InnerException, null) ? ex.InnerException.Message : ex.Message)); } }
/// <summary> /// Builds the attribute declaration objects to add to a class or property. /// </summary> /// <param name="nodes">The XmlNodes containing the attributes.</param> /// <returns>The CodeAttributeDeclarationCollection to add.</returns> public static CodeAttributeDeclarationCollection BuildCustomAttributes( ArrayList nodes) { CodeAttributeDeclarationCollection result = new CodeAttributeDeclarationCollection(); foreach (XmlNode node in nodes) { foreach (XmlNode child in node.ChildNodes) { if (child.LocalName == "CustomAttribute") { CodeAttributeDeclaration attr = new CodeAttributeDeclaration(); string[] namevalue = child.InnerText.Split('('); attr.Name = namevalue[0]; attr.Arguments.Add(new CodeAttributeArgument( new CodeSnippetExpression(namevalue[1].Substring(0, namevalue[1].Length - 1)))); result.Add(attr); } } } return(result); }
private CodeTypeDeclaration CreateThisAssemblyClass() { var thisAssembly = new CodeTypeDeclaration("ThisAssembly") { IsClass = true, IsPartial = true, TypeAttributes = TypeAttributes.NotPublic | TypeAttributes.Sealed, }; var codeAttributeDeclarationCollection = new CodeAttributeDeclarationCollection(); codeAttributeDeclarationCollection.Add(new CodeAttributeDeclaration("System.CodeDom.Compiler.GeneratedCode", new CodeAttributeArgument(new CodePrimitiveExpression(GeneratorName)), new CodeAttributeArgument(new CodePrimitiveExpression(GeneratorVersion)))); thisAssembly.CustomAttributes = codeAttributeDeclarationCollection; // CodeDOM doesn't support static classes, so hide the constructor instead. thisAssembly.Members.Add(new CodeConstructor { Attributes = MemberAttributes.Private }); // Determine information about the public key used in the assembly name. string publicKey, publicKeyToken; bool hasKeyInfo = this.TryReadKeyInfo(out publicKey, out publicKeyToken); // Define the constants. thisAssembly.Members.AddRange(CreateFields(new Dictionary <string, string> { { "AssemblyVersion", this.AssemblyVersion }, { "AssemblyFileVersion", this.AssemblyFileVersion }, { "AssemblyInformationalVersion", this.AssemblyInformationalVersion }, { "AssemblyName", this.AssemblyName }, { "AssemblyTitle", this.AssemblyTitle }, { "AssemblyProduct", this.AssemblyProduct }, { "AssemblyCopyright", this.AssemblyCopyright }, { "AssemblyCompany", this.AssemblyCompany }, { "AssemblyConfiguration", this.AssemblyConfiguration }, { "GitCommitId", this.GitCommitId }, }).ToArray()); if (long.TryParse(this.GitCommitDateTicks, out long gitCommitDateTicks)) { thisAssembly.Members.AddRange(CreateCommitDateProperty(gitCommitDateTicks).ToArray()); } if (hasKeyInfo) { thisAssembly.Members.AddRange(CreateFields(new Dictionary <string, string> { { "PublicKey", publicKey }, { "PublicKeyToken", publicKeyToken }, }).ToArray()); } // These properties should be defined even if they are empty. thisAssembly.Members.Add(CreateField("RootNamespace", this.RootNamespace)); return(thisAssembly); }
private void SetDescription(CodeAttributeDeclarationCollection customAttributes, string description) { customAttributes.Add( new CodeAttributeDeclaration( new CodeTypeReference(DESCRIPTION_ATTR), new CodeAttributeArgument( new CodePrimitiveExpression(description)))); }
public void CloneCustomAttributes(CodeAttributeDeclarationCollection source, CodeAttributeDeclarationCollection dest) { dest.Clear(); foreach (CodeAttributeDeclaration decl in source) { dest.Add(CloneCustomAttribute(decl)); } }
private static void AddClassInterfaceNone(CodeAttributeDeclarationCollection attrs) { attrs.Add(new CodeAttributeDeclaration( new CodeTypeReference("System.Runtime.InteropServices.ClassInterface"), new[] { new CodeAttributeArgument(new CodeFieldReferenceExpression( new CodeTypeReferenceExpression("System.Runtime.InteropServices.ClassInterfaceType"), ClassInterfaceType.None.ToString())) })); }
/// <summary> /// Generates code for the given custom attributes and adds them to the given <see cref="CodeAttributeDeclarationCollection"/> /// </summary> /// <param name="proxyGenerator">Root client proxy generator</param> /// <param name="referencingType">The referencing type</param> /// <param name="getLogWarningMessage">The function to call to get the warning message to be logged</param> /// <param name="attributes">Collection of attributes to generate</param> /// <param name="outputCollection">The collection to which the generated attributes will be added</param> /// <param name="comments">Collection of comments that should be updated if errors are discovered.</param> /// <param name="customCommentHeader">A custom comment header that will be displayed for any generated comment errors.</param> /// <param name="forcePropagation">Indicates whether or not to force attribute propagation.</param> public static void GenerateCustomAttributes(CodeDomClientCodeGenerator proxyGenerator, CodeTypeDeclaration referencingType, Func <AttributeBuilderException, string> getLogWarningMessage, IEnumerable <Attribute> attributes, CodeAttributeDeclarationCollection outputCollection, CodeCommentStatementCollection comments, string customCommentHeader, bool forcePropagation) { IEnumerable <CodeAttributeDeclaration> cads = GenerateCustomAttributes(proxyGenerator, referencingType, getLogWarningMessage, attributes, comments, customCommentHeader, forcePropagation); foreach (var cad in cads) { outputCollection.Add(cad); } }
static void PopulateCustomAttributes(ICustomAttributeProvider type, CodeAttributeDeclarationCollection attributes, Func <CodeTypeReference, CodeTypeReference> codeTypeModifier) { foreach (var customAttribute in type.CustomAttributes.Where(ShouldIncludeAttribute).OrderBy(a => a.AttributeType.FullName).ThenBy(a => ConvertAttrbuteToCode(codeTypeModifier, a))) { var attribute = GenerateCodeAttributeDeclaration(codeTypeModifier, customAttribute); attributes.Add(attribute); } }
private static void CopyAttrs(IList <CustomAttributeData> list, CodeAttributeDeclarationCollection newAttrs) { foreach (CustomAttributeData attrData in list) { CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration(new CodeTypeReference(attrData.Constructor.DeclaringType)); foreach (CustomAttributeTypedArgument arg in attrData.ConstructorArguments) { CodeExpression expr = null; if (arg.ArgumentType.IsEnum) { expr = new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(arg.ArgumentType.FullName), Enum.ToObject(arg.ArgumentType, arg.Value).ToString()); } else if (arg.ArgumentType == typeof(string) || arg.ArgumentType == typeof(int) || arg.ArgumentType == typeof(short)) { expr = new CodePrimitiveExpression(arg.Value); } else { expr = new CodeDefaultValueExpression(new CodeTypeReference(arg.ArgumentType)); } if (expr != null) { attrDec.Arguments.Add(new CodeAttributeArgument(expr)); } } foreach (CustomAttributeNamedArgument arg in attrData.NamedArguments) { CodeExpression expr = null; if (arg.MemberInfo is FieldInfo && Equals(arg.TypedValue.Value, 0) || Equals(arg.TypedValue.Value, (short)0)) { continue; } if (arg.TypedValue.ArgumentType.IsEnum) { expr = new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(arg.TypedValue.ArgumentType.FullName), Enum.ToObject(arg.TypedValue.ArgumentType, arg.TypedValue.Value).ToString()); } else if (arg.TypedValue.ArgumentType == typeof(string) || arg.TypedValue.ArgumentType == typeof(int) || arg.TypedValue.ArgumentType == typeof(short)) { expr = new CodePrimitiveExpression(arg.TypedValue.Value); } else { expr = new CodeDefaultValueExpression(new CodeTypeReference(arg.TypedValue.ArgumentType)); } if (expr != null) { attrDec.Arguments.Add(new CodeAttributeArgument(arg.MemberInfo.Name, expr)); } } //attr. newAttrs.Add(attrDec); } }