public sealed override Exception CreateMissingMetadataException(Type pertainant) { return(MissingMetadataExceptionCreator.Create(pertainant)); }
public sealed override Exception CreateMissingMetadataException(TypeInfo pertainant, string nestedTypeName) { return(MissingMetadataExceptionCreator.Create(pertainant, nestedTypeName)); }
// // Turn a CustomAttributeData into a live Attribute object. There's nothing actually non-portable about this one, // however, it is included as a concession to that the fact the Reflection.Execution which implements this contract // also needs this functionality to implement default values, and we don't want to duplicate this code. // public static Attribute Instantiate(this CustomAttributeData cad) { if (cad == null) { return(null); } Type attributeType = cad.AttributeType; TypeInfo attributeTypeInfo = attributeType.GetTypeInfo(); // // Find the public constructor that matches the supplied arguments. // ConstructorInfo matchingCtor = null; ParameterInfo[] matchingParameters = null; IList <CustomAttributeTypedArgument> constructorArguments = cad.ConstructorArguments; foreach (ConstructorInfo ctor in attributeTypeInfo.DeclaredConstructors) { if ((ctor.Attributes & (MethodAttributes.Static | MethodAttributes.MemberAccessMask)) != (MethodAttributes.Public)) { continue; } ParameterInfo[] parameters = ctor.GetParameters(); if (parameters.Length != constructorArguments.Count) { continue; } int i; for (i = 0; i < parameters.Length; i++) { Type parameterType = parameters[i].ParameterType; if (!(parameterType.Equals(constructorArguments[i].ArgumentType) || parameterType.Equals(typeof(Object)))) { break; } } if (i == parameters.Length) { matchingCtor = ctor; matchingParameters = parameters; break; } } if (matchingCtor == null) { throw MissingMetadataExceptionCreator.Create(attributeTypeInfo); // No matching ctor. } // // Found the right constructor. Instantiate the Attribute. // int arity = matchingParameters.Length; Object[] invokeArguments = new Object[arity]; for (int i = 0; i < arity; i++) { invokeArguments[i] = constructorArguments[i].Convert(); } Attribute newAttribute = (Attribute)(matchingCtor.Invoke(invokeArguments)); // // If there any named arguments, evaluate them and set the appropriate field or property. // foreach (CustomAttributeNamedArgument namedArgument in cad.NamedArguments) { Object argumentValue = namedArgument.TypedValue.Convert(); TypeInfo walk = attributeTypeInfo; String name = namedArgument.MemberName; if (namedArgument.IsField) { // Field for (; ;) { FieldInfo fieldInfo = walk.GetDeclaredField(name); if (fieldInfo != null) { fieldInfo.SetValue(newAttribute, argumentValue); break; } Type baseType = walk.BaseType; if (baseType == null) { throw MissingMetadataExceptionCreator.Create(attributeTypeInfo); // No field matches named argument. } walk = baseType.GetTypeInfo(); } } else { // Property for (; ;) { PropertyInfo propertyInfo = walk.GetDeclaredProperty(name); if (propertyInfo != null) { propertyInfo.SetValue(newAttribute, argumentValue); break; } Type baseType = walk.BaseType; if (baseType == null) { throw MissingMetadataExceptionCreator.Create(attributeTypeInfo); // No field matches named argument. } walk = baseType.GetTypeInfo(); } } } return(newAttribute); }