Example #1
0
        public static string FormatMethodName(MetadataReader metadataReader, Handle methodHandle)
        {
            MethodNameFormatter formatter = new MethodNameFormatter(metadataReader, SigTypeContext.FromMethod(metadataReader, methodHandle));

            formatter.EmitMethodName(methodHandle);
            return(formatter._outputBuilder.ToString());
        }
Example #2
0
        static void EnsureSatisfiesClassConstraints(TypeInfo[] typeParameters, TypeInfo[] typeArguments, object definition, SigTypeContext typeContext)
        {
            if (typeParameters.Length != typeArguments.Length)
            {
                throw new ArgumentException(SR.Argument_GenericArgsCount);
            }

            // Do sanity validation of all arguments first. The actual constraint validation can fail in unexpected ways 
            // if it hits SigTypeContext with these never valid types.
            for (int i = 0; i < typeParameters.Length; i++)
            {
                TypeInfo actualArg = typeArguments[i];

                if (actualArg.IsSystemVoid() || (actualArg.HasElementType && !actualArg.IsArray))
                {
                    throw new ArgumentException(SR.Format(SR.Argument_NeverValidGenericArgument, actualArg));
                }
            }

            for (int i = 0; i < typeParameters.Length; i++)
            {
                TypeInfo formalArg = typeParameters[i];
                TypeInfo actualArg = typeArguments[i];

                if (!formalArg.SatisfiesConstraints(typeContext, actualArg))
                {
                    throw new ArgumentException(SR.Format(SR.Argument_ConstraintFailed, actualArg, definition.ToString(), formalArg),
                        String.Format("GenericArguments[{0}]", i));
                }
            }
        }
Example #3
0
        public static string FormatMethodName(MetadataReader metadataReader, TypeDefinitionHandle enclosingTypeHandle, MethodHandle methodHandle)
        {
            MethodNameFormatter formatter = new MethodNameFormatter(metadataReader, SigTypeContext.FromMethod(metadataReader, enclosingTypeHandle, methodHandle));

            Method          method          = metadataReader.GetMethod(methodHandle);
            MethodSignature methodSignature = metadataReader.GetMethodSignature(method.Signature);

            formatter.EmitTypeName(enclosingTypeHandle, namespaceQualified: true);
            formatter._outputBuilder.Append('.');
            formatter.EmitString(method.Name);

            bool first = true;

            foreach (GenericParameterHandle handle in method.GenericParameters)
            {
                if (first)
                {
                    first = false;
                    formatter._outputBuilder.Append('[');
                }
                else
                {
                    formatter._outputBuilder.Append(", ");
                }
                formatter.EmitTypeName(handle, namespaceQualified: false);
            }
            if (!first)
            {
                formatter._outputBuilder.Append(']');
            }

            formatter.EmitMethodParameters(methodSignature);

            return(formatter._outputBuilder.ToString());
        }
        private static Type Instantiate(this Type type, SigTypeContext context)
        {
            if (type.IsGenericParameter)
            {
                int position = type.GenericParameterPosition;
                if (type.DeclaringMethod != null)
                {
                    return(context.MethodInstantiation[position]);
                }
                else
                {
                    Debug.Assert(type.DeclaringType != null);
                    return(context.TypeInstantiation[position]);
                }
            }

            if (type.ContainsGenericParameters)
            {
                //
                // Note we can come here for both generic and non-generic types. Consider this example:
                //
                // interface IFoo<T> { }
                // class Foo<U> : IFoo<U[]> { }
                //
                // var foo = typeof(Foo<>);
                // var ifoo = foo.ImplementedInterfaces.First();
                // var arg = ifoo.GetGenericArguments()[0];
                //
                // arg.ContainsGenericParameters will be true, but arg.IsGenericType will be false.
                //
                return(new InstantiatedTypeInfo(type, context));
            }

            return(type);
        }
        private static TypeInfo Instantiate(this TypeInfo type, SigTypeContext context)
        {
            if (type.IsGenericParameter)
            {
                int position = type.GenericParameterPosition;
                if (type.DeclaringMethod != null)
                {
                    return context.MethodInstantiation[position];
                }
                else
                {
                    Debug.Assert(type.DeclaringType != null);
                    return context.TypeInstantiation[position];
                }
            }

            if (type.ContainsGenericParameters)
            {
                //
                // Note we can come here for both generic and non-generic types. Consider this example:
                //
                // interface IFoo<T> { }
                // class Foo<U> : IFoo<U[]> { }
                //
                // var foo = typeof(Foo<>).GetTypeInfo();
                // var ifoo = foo.ImplementedInterfaces.First().GetTypeInfo();
                // var arg = ifoo.GetGenericArguments()[0].GetTypeInfo();
                //
                // arg.ContainsGenericParameters will be true, but arg.IsGenericType will be false.
                //
                return new InstantiatedTypeInfo(type, context);
            }

            return type;
        }
Example #6
0
        public static void EnsureSatisfiesClassConstraints(TypeInfo typeDefinition, TypeInfo[] typeArguments)
        {
            TypeInfo[]     typeParameters = TypesToTypeInfos(typeDefinition.GenericTypeParameters);
            SigTypeContext typeContext    = new SigTypeContext(typeArguments, null);

            EnsureSatisfiesClassConstraints(typeParameters, typeArguments, typeDefinition, typeContext);
        }
        public static void EnsureSatisfiesClassConstraints(Type typeDefinition, Type[] typeArguments)
        {
            Type[]         typeParameters = typeDefinition.GetGenericArguments();
            SigTypeContext typeContext    = new SigTypeContext(typeArguments, null);

            EnsureSatisfiesClassConstraints(typeParameters, typeArguments, typeDefinition, typeContext);
        }
Example #8
0
        private static bool SatisfiesConstraints(this TypeInfo genericVariable, SigTypeContext typeContextOfConstraintDeclarer, TypeInfo typeArg)
        {
            GenericParameterAttributes specialConstraints = genericVariable.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;

            if ((specialConstraints & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
            {
                if (!typeArg.IsValueType)
                {
                    return(false);
                }
                else
                {
                    // the type argument is a value type, however if it is any kind of Nullable we want to fail
                    // as the constraint accepts any value type except Nullable types (Nullable itself is a value type)
                    if (typeArg.IsNullable())
                    {
                        return(false);
                    }
                }
            }

            if ((specialConstraints & GenericParameterAttributes.ReferenceTypeConstraint) != 0)
            {
                if (typeArg.IsValueType)
                {
                    return(false);
                }
            }

            if ((specialConstraints & GenericParameterAttributes.DefaultConstructorConstraint) != 0)
            {
                if (!typeArg.HasExplicitOrImplicitPublicDefaultConstructor())
                {
                    return(false);
                }
            }

            // Now check general subtype constraints
            foreach (var constraint in genericVariable.GetGenericParameterConstraints())
            {
                TypeInfo typeConstraint = constraint.GetTypeInfo();

                TypeInfo instantiatedTypeConstraint = typeConstraint.Instantiate(typeContextOfConstraintDeclarer);

                // System.Object constraint will be always satisfied - even if argList is empty
                if (instantiatedTypeConstraint.IsSystemObject())
                {
                    continue;
                }

                // if a concrete type can be cast to the constraint, then this constraint will be satisifed
                if (!AreTypesAssignable(typeArg, instantiatedTypeConstraint))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #9
0
 public static void EnsureSatisfiesClassConstraints(MethodInfo reflectionMethodInfo)
 {
     MethodInfo genericMethodDefinition = reflectionMethodInfo.GetGenericMethodDefinition();
     TypeInfo[] methodArguments = TypesToTypeInfos(reflectionMethodInfo.GetGenericArguments());
     TypeInfo[] methodParameters = TypesToTypeInfos(genericMethodDefinition.GetGenericArguments());
     TypeInfo[] typeArguments = TypesToTypeInfos(reflectionMethodInfo.DeclaringType.GetGenericArguments());
     SigTypeContext typeContext = new SigTypeContext(typeArguments, methodArguments);
     EnsureSatisfiesClassConstraints(methodParameters, methodArguments, genericMethodDefinition, typeContext);
 }
Example #10
0
        public static void EnsureSatisfiesClassConstraints(MethodInfo reflectionMethodInfo)
        {
            MethodInfo genericMethodDefinition = reflectionMethodInfo.GetGenericMethodDefinition();

            Type[]         methodArguments  = reflectionMethodInfo.GetGenericArguments();
            Type[]         methodParameters = genericMethodDefinition.GetGenericArguments();
            Type[]         typeArguments    = reflectionMethodInfo.DeclaringType.GetGenericArguments();
            SigTypeContext typeContext      = new SigTypeContext(typeArguments, methodArguments);

            EnsureSatisfiesClassConstraints(methodParameters, methodArguments, genericMethodDefinition, typeContext);
        }
Example #11
0
        static bool SatisfiesConstraints(this TypeInfo genericVariable, SigTypeContext typeContextOfConstraintDeclarer, TypeInfo typeArg)
        {
            GenericParameterAttributes specialConstraints = genericVariable.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;

            if ((specialConstraints & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
            {
                if (!typeArg.IsValueType)
                    return false;
                else
                {
                    // the type argument is a value type, however if it is any kind of Nullable we want to fail
                    // as the constraint accepts any value type except Nullable types (Nullable itself is a value type)
                    if (typeArg.IsNullable())
                        return false;
                }
            }

            if ((specialConstraints & GenericParameterAttributes.ReferenceTypeConstraint) != 0)
            {
                if (typeArg.IsValueType)
                    return false;
            }

            if ((specialConstraints & GenericParameterAttributes.DefaultConstructorConstraint) != 0)
            {
                if (!typeArg.HasExplicitOrImplicitPublicDefaultConstructor())
                    return false;
            }

            // Now check general subtype constraints
            foreach (var constraint in genericVariable.GetGenericParameterConstraints())
            {
                TypeInfo typeConstraint = constraint.GetTypeInfo();

                TypeInfo instantiatedTypeConstraint = typeConstraint.Instantiate(typeContextOfConstraintDeclarer);

                // System.Object constraint will be always satisfied - even if argList is empty
                if (instantiatedTypeConstraint.IsSystemObject())
                    continue;

                // if a concrete type can be cast to the constraint, then this constraint will be satisifed
                if (!AreTypesAssignable(typeArg, instantiatedTypeConstraint))
                    return false;
            }

            return true;
        }
 public InstantiatedTypeInfo(TypeInfo underlyingTypeInfo, SigTypeContext context)
 {
     _underlyingTypeInfo = underlyingTypeInfo;
     _context            = context;
 }
 public InstantiatedTypeInfo(Type underlyingType, SigTypeContext context)
 {
     _underlyingType = underlyingType;
     _context        = context;
 }
 public InstantiatedTypeInfo(TypeInfo underlyingTypeInfo, SigTypeContext context)
 {
     _underlyingTypeInfo = underlyingTypeInfo;
     _context = context;
 }
Example #15
0
 public static void EnsureSatisfiesClassConstraints(TypeInfo typeDefinition, TypeInfo[] typeArguments)
 {
     TypeInfo[] typeParameters = TypesToTypeInfos(typeDefinition.GenericTypeParameters);
     SigTypeContext typeContext = new SigTypeContext(typeArguments, null);
     EnsureSatisfiesClassConstraints(typeParameters, typeArguments, typeDefinition, typeContext);
 }
Example #16
0
 /// <summary>
 /// Initialize the reader used for method name formatting.
 /// </summary>
 private MethodNameFormatter(MetadataReader metadataReader, SigTypeContext typeContext)
 {
     _metadataReader = metadataReader;
     _outputBuilder  = new StringBuilder();
     _typeContext    = typeContext;
 }
 public InstantiatedTypeInfo(Type underlyingType, SigTypeContext context)
 {
     _underlyingType = underlyingType;
     _context = context;
 }
Example #18
0
        private static void EnsureSatisfiesClassConstraints(Type[] typeParameters, Type[] typeArguments, object definition, SigTypeContext typeContext)
        {
            if (typeParameters.Length != typeArguments.Length)
            {
                throw new ArgumentException(SR.Argument_GenericArgsCount);
            }

            // Do sanity validation of all arguments first. The actual constraint validation can fail in unexpected ways
            // if it hits SigTypeContext with these never valid types.
            for (int i = 0; i < typeParameters.Length; i++)
            {
                Type actualArg = typeArguments[i];

                if (actualArg.IsSystemVoid() || (actualArg.HasElementType && !actualArg.IsArray))
                {
                    throw new ArgumentException(SR.Format(SR.Argument_NeverValidGenericArgument, actualArg));
                }
            }

            for (int i = 0; i < typeParameters.Length; i++)
            {
                Type formalArg = typeParameters[i];
                Type actualArg = typeArguments[i];

                if (!formalArg.SatisfiesConstraints(typeContext, actualArg))
                {
                    throw new ArgumentException(SR.Format(SR.Argument_ConstraintFailed, actualArg, definition.ToString(), formalArg),
                                                String.Format("GenericArguments[{0}]", i));
                }
            }
        }
Example #19
0
 public static void EnsureSatisfiesClassConstraints(Type typeDefinition, Type[] typeArguments)
 {
     Type[] typeParameters = typeDefinition.GetGenericArguments();
     SigTypeContext typeContext = new SigTypeContext(typeArguments, null);
     EnsureSatisfiesClassConstraints(typeParameters, typeArguments, typeDefinition, typeContext);
 }