protected CustomAttribute BuildCustomAttribute(MethodReference ctor, CustomAttrib sig) { CustomAttribute cattr = new CustomAttribute(ctor); foreach (CustomAttrib.FixedArg fa in sig.FixedArgs) { cattr.ConstructorParameters.Add(GetFixedArgValue(fa)); } foreach (CustomAttrib.NamedArg na in sig.NamedArgs) { object value = GetFixedArgValue(na.FixedArg); if (na.Field) { cattr.Fields [na.FieldOrPropName] = value; cattr.SetFieldType(na.FieldOrPropName, GetFixedArgType(na.FixedArg)); } else if (na.Property) { cattr.Properties [na.FieldOrPropName] = value; cattr.SetPropertyType(na.FieldOrPropName, GetFixedArgType(na.FixedArg)); } else { throw new ReflectionException("Non valid named arg"); } } return(cattr); }
protected CustomAttribute BuildCustomAttribute(MethodReference ctor, CustomAttrib sig) { CustomAttribute cattr = new CustomAttribute (ctor); foreach (CustomAttrib.FixedArg fa in sig.FixedArgs) cattr.ConstructorParameters.Add (GetFixedArgValue (fa)); foreach (CustomAttrib.NamedArg na in sig.NamedArgs) { object value = GetFixedArgValue (na.FixedArg); if (na.Field) { cattr.Fields [na.FieldOrPropName] = value; cattr.SetFieldType (na.FieldOrPropName, GetNamedArgType (na)); } else if (na.Property) { cattr.Properties [na.FieldOrPropName] = value; cattr.SetPropertyType (na.FieldOrPropName, GetNamedArgType (na)); } else throw new ReflectionException ("Non valid named arg"); } return cattr; }
public static void addCustomAttributeToMethod(AssemblyDefinition assembly, string methodType,string methodToAdd, Type attributeType, Dictionary<System.String, System.String> fields) { var targetType = CecilUtils.getType(assembly, methodType); //var targetType = CecilUtils.getMethodsassembly,methodType, typeToAdd); if (targetType == null) { DI.log.error("in addCustomAttributeToType: could not find type {0} in assembly {1}", methodType, assembly.Name.Name); } else { var methods = CecilUtils.getMethods(targetType); foreach (var method in methods) { if (method.Name == methodToAdd) { var customAttribute = new CustomAttribute( assembly.MainModule.Import(attributeType.GetConstructor(new Type[] { }))); if (fields != null) foreach (var field in fields) { //customAttribute.SetFieldType(field.Key, assembly.MainModule.Import(typeof(System.String))); customAttribute.SetFieldType(field.Key, assembly.MainModule.Import(field.Value.GetType())); customAttribute.Fields[field.Key] = field.Value; } method.CustomAttributes.Add(customAttribute); } } } //assembly.CustomAttributes.Add(customAttribute); }
public override void VisitCustomAttribute (CustomAttribute customAttr) { customAttr.Constructor = GetMethodReference (customAttr.Constructor); Hashtable dict = new Hashtable (customAttr.Fields); foreach (DictionaryEntry entry in dict) customAttr.SetFieldType ( (string) entry.Key, GetTypeReference (customAttr.GetFieldType ((string) entry.Key))); dict = new Hashtable (customAttr.Properties); foreach (DictionaryEntry entry in dict) customAttr.SetPropertyType ( (string) entry.Key, GetTypeReference (customAttr.GetPropertyType ((string) entry.Key))); }
/*public static void addXTraceMethodInvocationAttribute(AssemblyDefinition assembly, string targetAssemblyName, string targetNamespace) { addCustomAttributeToAssembly(assembly, typeof(OnMethodInvocationAttribute), new Dictionary<System.String, System.String>() { { "AttributeTargetAssemblies", targetAssemblyName}, // "mscorlib"}, { "AttributeTargetTypes", targetNamespace} //"System.Console.*"} }); }*/ public static void addCustomAttributeToAssembly(AssemblyDefinition assembly, Type attributeType, Dictionary<System.String, System.String> fields) { var customAttribute = new CustomAttribute( assembly.MainModule.Import(attributeType.GetConstructor(new Type[] { }))); if (fields!=null) foreach (var field in fields) { //customAttribute.SetFieldType(field.Key, assembly.MainModule.Import(typeof(System.String))); customAttribute.SetFieldType(field.Key, assembly.MainModule.Import(field.Value.GetType())); customAttribute.Fields[field.Key] = field.Value; } assembly.CustomAttributes.Add(customAttribute); }