public sealed override Exception CreateNonInvokabilityException(MemberInfo pertainant)
        {
            String resourceName = SR.Object_NotInvokable;

            if (pertainant is MethodBase)
            {
                MethodBase methodBase = (MethodBase)pertainant;
                resourceName = (methodBase.IsGenericMethod && !methodBase.IsGenericMethodDefinition) ? SR.MakeGenericMethod_NoMetadata : SR.Object_NotInvokable;
                if (methodBase is ConstructorInfo)
                {
                    TypeInfo declaringTypeInfo = methodBase.DeclaringType.GetTypeInfo();
                    if (typeof(Delegate).GetTypeInfo().IsAssignableFrom(declaringTypeInfo))
                    {
                        throw new PlatformNotSupportedException(SR.PlatformNotSupported_CannotInvokeDelegateCtor);
                    }
                }
            }
            String pertainantString = MissingMetadataExceptionCreator.ComputeUsefulPertainantIfPossible(pertainant);

            if (pertainantString == null)
            {
                pertainantString = "?";
            }
            return(new MissingRuntimeArtifactException(SR.Format(resourceName, pertainantString)));
        }
 public sealed override Exception CreateMissingConstructedGenericTypeException(Type genericTypeDefinition, Type[] genericTypeArguments)
 {
     return(MissingMetadataExceptionCreator.CreateMissingConstructedGenericTypeException(genericTypeDefinition, genericTypeArguments));
 }
 public sealed override Exception CreateMissingArrayTypeException(Type elementType, bool isMultiDim, int rank)
 {
     return(MissingMetadataExceptionCreator.CreateMissingArrayTypeException(elementType, isMultiDim, rank));
 }
 public sealed override Exception CreateMissingMetadataException(TypeInfo pertainant, string nestedTypeName)
 {
     return(MissingMetadataExceptionCreator.Create(pertainant, nestedTypeName));
 }
 public sealed override Exception CreateMissingMetadataException(Type pertainant)
 {
     return(MissingMetadataExceptionCreator.Create(pertainant));
 }
Exemplo n.º 6
0
        //
        // 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);
        }