public List <MA.CustomAttribute> GetRawCustomAttributes(object obj, Type type, bool inherit) { List <MA.CustomAttribute> atts = new List <MA.CustomAttribute> (); Mono.Cecil.ICustomAttributeProvider aprov = obj as Mono.Cecil.ICustomAttributeProvider; if (aprov == null) { return(atts); } foreach (CustomAttribute att in aprov.CustomAttributes) { // The class of the attribute is always a subclass of a Mono.Addins class MA.CustomAttribute catt = ConvertToRawAttribute(att, type.FullName, baseIsMonoAddinsType: true); if (catt != null) { atts.Add(catt); } } if (inherit && (obj is TypeDefinition)) { TypeDefinition td = (TypeDefinition)obj; if (td.BaseType != null && td.BaseType.FullName != "System.Object") { // The base type may be in an assembly that doesn't reference Mono.Addins, even though it may reference // other assemblies that do reference Mono.Addins. So the Mono.Addins filter can't be applied here. TypeDefinition bt = FindTypeDefinition(td.Module.Assembly, td.BaseType, assembliesReferencingMonoAddinsOnly: false); if (bt != null) { atts.AddRange(GetRawCustomAttributes(bt, type, true)); } } } return(atts); }
public List <MA.CustomAttribute> GetRawCustomAttributes(object obj, Type type, bool inherit) { List <MA.CustomAttribute> atts = new List <MA.CustomAttribute> (); Mono.Cecil.ICustomAttributeProvider aprov = obj as Mono.Cecil.ICustomAttributeProvider; if (aprov == null) { return(atts); } foreach (CustomAttribute att in aprov.CustomAttributes) { MA.CustomAttribute catt = ConvertToRawAttribute(att, type.FullName); if (catt != null) { atts.Add(catt); } } if (inherit && (obj is TypeDefinition)) { TypeDefinition td = (TypeDefinition)obj; if (td.BaseType != null && td.BaseType.FullName != "System.Object") { TypeDefinition bt = FindTypeDefinition(td.Module.Assembly, td.BaseType); if (bt != null) { atts.AddRange(GetRawCustomAttributes(bt, type, true)); } } } return(atts); }
MA.CustomAttribute ConvertToRawAttribute(CustomAttribute att, string expectedType) { TypeDefinition attType = FindTypeDefinition(att.Constructor.DeclaringType.Module.Assembly, att.Constructor.DeclaringType); if (attType == null || !TypeIsAssignableFrom(expectedType, attType)) { return(null); } MA.CustomAttribute mat = new MA.CustomAttribute(); mat.TypeName = att.Constructor.DeclaringType.FullName; if (att.ConstructorArguments.Count > 0) { var arguments = att.ConstructorArguments; MethodReference constructor = FindConstructor(att); if (constructor == null) { throw new InvalidOperationException("Custom attribute constructor not found"); } for (int n = 0; n < arguments.Count; n++) { ParameterDefinition par = constructor.Parameters[n]; object val = arguments [n].Value; if (val != null) { string name = par.Name; NodeAttributeAttribute bat = (NodeAttributeAttribute)GetCustomAttribute(par, typeof(NodeAttributeAttribute), false); if (bat != null) { name = bat.Name; } mat.Add(name, Convert.ToString(val, System.Globalization.CultureInfo.InvariantCulture)); } } } foreach (Mono.Cecil.CustomAttributeNamedArgument namedArgument in att.Properties) { string pname = namedArgument.Name; object val = namedArgument.Argument.Value; if (val == null) { continue; } foreach (TypeDefinition td in GetInheritanceChain(attType)) { PropertyDefinition prop = GetMember(td.Properties, pname); if (prop == null) { continue; } NodeAttributeAttribute bat = (NodeAttributeAttribute)GetCustomAttribute(prop, typeof(NodeAttributeAttribute), false); if (bat != null) { string name = string.IsNullOrEmpty(bat.Name) ? prop.Name : bat.Name; mat.Add(name, Convert.ToString(val, System.Globalization.CultureInfo.InvariantCulture)); } } } foreach (Mono.Cecil.CustomAttributeNamedArgument namedArgument in att.Fields) { string pname = namedArgument.Name; object val = namedArgument.Argument.Value; if (val == null) { continue; } foreach (TypeDefinition td in GetInheritanceChain(attType)) { FieldDefinition field = GetMember(td.Fields, pname); if (field != null) { NodeAttributeAttribute bat = (NodeAttributeAttribute)GetCustomAttribute(field, typeof(NodeAttributeAttribute), false); if (bat != null) { string name = string.IsNullOrEmpty(bat.Name) ? field.Name : bat.Name; mat.Add(name, Convert.ToString(val, System.Globalization.CultureInfo.InvariantCulture)); } } } } return(mat); }
MA.CustomAttribute ConvertToRawAttribute(CustomAttribute att, string expectedType) { TypeDefinition attType = FindTypeDefinition (att.Constructor.DeclaringType.Module.Assembly, att.Constructor.DeclaringType); if (attType == null || !TypeIsAssignableFrom (expectedType, attType)) return null; MA.CustomAttribute mat = new MA.CustomAttribute (); mat.TypeName = att.Constructor.DeclaringType.FullName; if (att.ConstructorParameters.Count > 0) { IList cargs = att.ConstructorParameters; MethodReference constructor = FindConstructor (att); if (constructor == null) throw new InvalidOperationException ("Custom attribute constructor not found"); for (int n=0; n<cargs.Count; n++) { ParameterDefinition par = constructor.Parameters[n]; object val = cargs [n]; if (val != null) { string name = par.Name; NodeAttributeAttribute bat = (NodeAttributeAttribute) GetCustomAttribute (par, typeof(NodeAttributeAttribute), false); if (bat != null) name = bat.Name; mat.Add (name, Convert.ToString (val, System.Globalization.CultureInfo.InvariantCulture)); } } } foreach (DictionaryEntry de in att.Properties) { string pname = (string)de.Key; object val = de.Value; if (val == null) continue; foreach (TypeDefinition td in GetInheritanceChain (attType)) { bool propFound = false; foreach (PropertyDefinition prop in td.Properties.GetProperties (pname)) { NodeAttributeAttribute bat = (NodeAttributeAttribute) GetCustomAttribute (prop, typeof(NodeAttributeAttribute), false); if (bat != null) { string name = string.IsNullOrEmpty (bat.Name) ? prop.Name : bat.Name; mat.Add (name, Convert.ToString (val, System.Globalization.CultureInfo.InvariantCulture)); propFound = true; break; } } if (propFound) break; } } foreach (DictionaryEntry de in att.Fields) { string pname = (string)de.Key; object val = de.Value; if (val == null) continue; foreach (TypeDefinition td in GetInheritanceChain (attType)) { FieldDefinition field = td.Fields.GetField (pname); if (field != null) { NodeAttributeAttribute bat = (NodeAttributeAttribute) GetCustomAttribute (field, typeof(NodeAttributeAttribute), false); if (bat != null) { string name = string.IsNullOrEmpty (bat.Name) ? field.Name : bat.Name; mat.Add (name, Convert.ToString (val, System.Globalization.CultureInfo.InvariantCulture)); } } } } return mat; }