public MethodSignatureComparer(
            QMethodDefinition methodHandle)
        {
            if (methodHandle.IsNativeFormatMetadataBased)
            {
                _metadataReader = methodHandle.NativeFormatReader;
                _methodHandle   = methodHandle.NativeFormatHandle;

                _method = _methodHandle.GetMethod(_metadataReader);

                _methodSignature = _method.Signature.GetMethodSignature(_metadataReader);
                _isGeneric       = (_methodSignature.GenericParameterCount != 0);

                // Precalculate initial method attributes used in signature queries
                _isStatic = (_method.Flags & MethodAttributes.Static) != 0;
            }
            else
            {
                _metadataReader  = null;
                _methodHandle    = default(MethodHandle);
                _method          = default(Method);
                _methodSignature = default(MethodSignature);
                _isGeneric       = false;
                _isStatic        = false;
            }
        }
Ejemplo n.º 2
0
        private bool GetAccessor(MethodSemanticsAttributes methodSemanticsAttribute, out MethodHandle methodHandle)
        {
            bool inherited = !_reflectedType.Equals(_contextTypeInfo);

            foreach (MethodSemanticsHandle methodSemanticsHandle in _property.MethodSemantics)
            {
                MethodSemantics methodSemantics = methodSemanticsHandle.GetMethodSemantics(_reader);
                if (methodSemantics.Attributes == methodSemanticsAttribute)
                {
                    methodHandle = methodSemantics.Method;

                    if (inherited)
                    {
                        MethodAttributes flags = methodHandle.GetMethod(_reader).Flags;
                        if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private)
                        {
                            continue;
                        }
                    }

                    return(true);
                }
            }
            methodHandle = default(MethodHandle);
            return(false);
        }
Ejemplo n.º 3
0
 //
 // methodHandle    - the "tkMethodDef" that identifies the method.
 // definingType   - the "tkTypeDef" that defined the method (this is where you get the metadata reader that created methodHandle.)
 // contextType    - the type that supplies the type context (i.e. substitutions for generic parameters.) Though you
 //                  get your raw information from "definingType", you report "contextType" as your DeclaringType property.
 //
 //  For example:
 //
 //       typeof(Foo<>).GetTypeInfo().DeclaredMembers
 //
 //           The definingType and contextType are both Foo<>
 //
 //       typeof(Foo<int,String>).GetTypeInfo().DeclaredMembers
 //
 //          The definingType is "Foo<,>"
 //          The contextType is "Foo<int,String>"
 //
 //  We don't report any DeclaredMembers for arrays or generic parameters so those don't apply.
 //
 public NativeFormatMethodCommon(MethodHandle methodHandle, NativeFormatRuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo)
 {
     _definingTypeInfo = definingTypeInfo;
     _methodHandle     = methodHandle;
     _contextTypeInfo  = contextTypeInfo;
     _reader           = definingTypeInfo.Reader;
     _method           = methodHandle.GetMethod(_reader);
 }
Ejemplo n.º 4
0
 //
 // methodHandle    - the "tkMethodDef" that identifies the method.
 // definingType   - the "tkTypeDef" that defined the method (this is where you get the metadata reader that created methodHandle.)
 // contextType    - the type that supplies the type context (i.e. substitutions for generic parameters.) Though you
 //                  get your raw information from "definingType", you report "contextType" as your DeclaringType property.
 //
 //  For example:
 //
 //       typeof(Foo<>).GetTypeInfo().DeclaredMembers
 //
 //           The definingType and contextType are both Foo<>
 //
 //       typeof(Foo<int,String>).GetTypeInfo().DeclaredMembers
 //
 //          The definingType is "Foo<,>"
 //          The contextType is "Foo<int,String>"
 //
 //  We don't report any DeclaredMembers for arrays or generic parameters so those don't apply.
 //
 public RuntimeMethodCommon(MethodHandle methodHandle, RuntimeNamedTypeInfo definingTypeInfo, RuntimeTypeInfo contextTypeInfo)
 {
     _definingTypeInfo = definingTypeInfo;
     _methodHandle = methodHandle;
     _contextTypeInfo = contextTypeInfo;
     _reader = definingTypeInfo.Reader;
     _method = methodHandle.GetMethod(_reader);
 }
Ejemplo n.º 5
0
        public NullableInstanceMethodInvoker(MetadataReader reader, MethodHandle methodHandle, RuntimeTypeHandle nullableTypeHandle, MethodInvokeInfo methodInvokeInfo)
        {
            _id = NullableMethodId.None;
            _nullableTypeHandle = nullableTypeHandle;
            Method method = methodHandle.GetMethod(reader);

            if (MethodAttributes.Public == (method.Flags & MethodAttributes.MemberAccessMask))
            {
                // Note: Since we control the definition of Nullable<>, we're not checking signatures here.
                String name = method.Name.GetConstantStringValue(reader).Value;
                switch (name)
                {
                case "GetType":
                    _id = NullableMethodId.GetType;
                    break;

                case "ToString":
                    _id = NullableMethodId.ToString;
                    break;

                case "Equals":
                    _id = NullableMethodId.Equals;
                    break;

                case "GetHashCode":
                    _id = NullableMethodId.GetHashCode;
                    break;

                case ".ctor":
                    _id = NullableMethodId.Ctor;
                    break;

                case "get_HasValue":
                    _id = NullableMethodId.get_HasValue;
                    break;

                case "get_Value":
                    _id = NullableMethodId.get_Value;
                    break;

                case "GetValueOrDefault":
                    IEnumerator <Handle> parameters = method.Signature.GetMethodSignature(reader).Parameters.GetEnumerator();
                    if (parameters.MoveNext())
                    {
                        _id = NullableMethodId.GetValueOrDefault_1;
                    }
                    else
                    {
                        _id = NullableMethodId.GetValueOrDefault_0;
                    }
                    break;

                default:
                    break;
                }
            }
        }
        public sealed override IEnumerable <CustomAttributeData> GetPsuedoCustomAttributes(MetadataReader reader, MethodHandle methodHandle, TypeDefinitionHandle declaringTypeHandle)
        {
            MethodImplAttributes implAttributes = methodHandle.GetMethod(reader).ImplFlags;

            if (0 != (implAttributes & MethodImplAttributes.PreserveSig))
            {
                yield return(ReflectionCoreExecution.ExecutionDomain.GetCustomAttributeData(typeof(PreserveSigAttribute), null, null));
            }
        }
 //
 // Creates the appropriate flavor of Invoker depending on the calling convention "shape" (static, instance or virtual.)
 //
 internal static MethodInvoker CreateMethodInvoker(MetadataReader reader, RuntimeTypeHandle declaringTypeHandle, MethodHandle methodHandle, MethodInvokeInfo methodInvokeInfo)
 {
     Method method = methodHandle.GetMethod(reader);
     MethodAttributes methodAttributes = method.Flags;
     if (0 != (methodAttributes & MethodAttributes.Static))
         return new StaticMethodInvoker(methodInvokeInfo);
     else if (methodInvokeInfo.VirtualResolveData != IntPtr.Zero)
         return new VirtualMethodInvoker(methodInvokeInfo, declaringTypeHandle);
     else
         return new InstanceMethodInvoker(methodInvokeInfo, declaringTypeHandle);
 }
        public NullableInstanceMethodInvoker(MetadataReader reader, MethodHandle methodHandle, RuntimeTypeHandle nullableTypeHandle, MethodInvokeInfo methodInvokeInfo)
        {
            _id = NullableMethodId.None;
            _nullableTypeHandle = nullableTypeHandle;
            Method method = methodHandle.GetMethod(reader);
            if (MethodAttributes.Public == (method.Flags & MethodAttributes.MemberAccessMask))
            {
                // Note: Since we control the definition of Nullable<>, we're not checking signatures here.
                String name = method.Name.GetConstantStringValue(reader).Value;
                switch (name)
                {
                    case "GetType":
                        _id = NullableMethodId.GetType;
                        break;

                    case "ToString":
                        _id = NullableMethodId.ToString;
                        break;

                    case "Equals":
                        _id = NullableMethodId.Equals;
                        break;

                    case "GetHashCode":
                        _id = NullableMethodId.GetHashCode;
                        break;

                    case ".ctor":
                        _id = NullableMethodId.Ctor;
                        break;

                    case "get_HasValue":
                        _id = NullableMethodId.get_HasValue;
                        break;

                    case "get_Value":
                        _id = NullableMethodId.get_Value;
                        break;

                    case "GetValueOrDefault":
                        IEnumerator<ParameterTypeSignatureHandle> parameters = method.Signature.GetMethodSignature(reader).Parameters.GetEnumerator();
                        if (parameters.MoveNext())
                            _id = NullableMethodId.GetValueOrDefault_1;
                        else
                            _id = NullableMethodId.GetValueOrDefault_0;
                        break;

                    default:
                        break;
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Emit parenthesized method argument type list with parameter names.
        /// </summary>
        /// <param name="methodHandle">Method handle to use for parameter formatting</param>
        private void EmitMethodParameters(MethodHandle methodHandle)
        {
            bool TryGetNextParameter(ref ParameterHandleCollection.Enumerator enumerator, out Parameter parameter)
            {
                bool hasNext = enumerator.MoveNext();

                parameter = hasNext ? enumerator.Current.GetParameter(_metadataReader) : default;
                return(hasNext);
            }

            Method           method     = methodHandle.GetMethod(_metadataReader);
            HandleCollection typeVector = method.Signature.GetMethodSignature(_metadataReader).Parameters;

            ParameterHandleCollection.Enumerator parameters = method.Parameters.GetEnumerator();

            bool hasParameter = TryGetNextParameter(ref parameters, out Parameter parameter);

            if (hasParameter && parameter.Sequence == 0)
            {
                hasParameter = TryGetNextParameter(ref parameters, out parameter);
            }

            _outputBuilder.Append('(');

            uint typeIndex = 0;

            foreach (Handle type in typeVector)
            {
                if (typeIndex != 0)
                {
                    _outputBuilder.Append(", ");
                }

                EmitTypeName(type, namespaceQualified: false);

                if (++typeIndex == parameter.Sequence && hasParameter)
                {
                    string name = parameter.Name.GetConstantStringValue(_metadataReader).Value;
                    hasParameter = TryGetNextParameter(ref parameters, out parameter);

                    if (!string.IsNullOrEmpty(name))
                    {
                        _outputBuilder.Append(' ');
                        _outputBuilder.Append(name);
                    }
                }
            }

            _outputBuilder.Append(')');
        }
        /// <summary>
        /// Construct a comparer between NativeFormat metadata methods and native layouts
        /// </summary>
        /// <param name="metadataReader">Metadata reader for the method declaring type</param>
        /// <param name="methodHandle">Handle of method to compare</param>
        public MethodSignatureComparer(
            MetadataReader metadataReader,
            MethodHandle methodHandle)
        {
            _metadataReader = metadataReader;
            _methodHandle   = methodHandle;

            _method = methodHandle.GetMethod(metadataReader);

            _methodSignature = _method.Signature.GetMethodSignature(_metadataReader);
            _isGeneric       = (_methodSignature.GenericParameterCount != 0);

            // Precalculate initial method attributes used in signature queries
            _isStatic = (_method.Flags & MethodAttributes.Static) != 0;
        }
        /// <summary>
        /// Construct a comparer between NativeFormat metadata methods and native layouts
        /// </summary>
        /// <param name="metadataReader">Metadata reader for the method declaring type</param>
        /// <param name="methodHandle">Handle of method to compare</param>
        public MethodSignatureComparer(
            MetadataReader metadataReader,
            MethodHandle methodHandle)
        {
            _metadataReader = metadataReader;
            _methodHandle = methodHandle;

            _method = methodHandle.GetMethod(metadataReader);

            _methodSignature = _method.Signature.GetMethodSignature(_metadataReader);
            _isGeneric = (_methodSignature.GenericParameterCount != 0);

            // Precalculate initial method attributes used in signature queries
            _isStatic = (_method.Flags & MethodAttributes.Static) != 0;
        }
Ejemplo n.º 12
0
        public RuntimeTypeInfo[] GetGenericTypeParametersWithSpecifiedOwningMethod(RuntimeNamedMethodInfo<NativeFormatMethodCommon> owningMethod)
        {
            Method method = MethodHandle.GetMethod(Reader);
            int genericParametersCount = method.GenericParameters.Count;
            if (genericParametersCount == 0)
                return Array.Empty<RuntimeTypeInfo>();

            RuntimeTypeInfo[] genericTypeParameters = new RuntimeTypeInfo[genericParametersCount];
            int i = 0;
            foreach (GenericParameterHandle genericParameterHandle in method.GenericParameters)
            {
                RuntimeTypeInfo genericParameterType = NativeFormatRuntimeGenericParameterTypeInfoForMethods.GetRuntimeGenericParameterTypeInfoForMethods(owningMethod, Reader, genericParameterHandle);
                genericTypeParameters[i++] = genericParameterType;
            }
            return genericTypeParameters;
        }
        //
        // Creates the appropriate flavor of Invoker depending on the calling convention "shape" (static, instance or virtual.)
        //
        internal static MethodInvoker CreateMethodInvoker(MetadataReader reader, RuntimeTypeHandle declaringTypeHandle, MethodHandle methodHandle, MethodInvokeInfo methodInvokeInfo)
        {
            Method           method           = methodHandle.GetMethod(reader);
            MethodAttributes methodAttributes = method.Flags;

            if (0 != (methodAttributes & MethodAttributes.Static))
            {
                return(new StaticMethodInvoker(methodInvokeInfo));
            }
            else if (methodInvokeInfo.VirtualResolveData != IntPtr.Zero)
            {
                return(new VirtualMethodInvoker(methodInvokeInfo, declaringTypeHandle));
            }
            else
            {
                return(new InstanceMethodInvoker(methodInvokeInfo, declaringTypeHandle));
            }
        }
Ejemplo n.º 14
0
        public IntPtrConstructorMethodInvoker(MetadataReader reader, MethodHandle methodHandle)
        {
            // Since we control the definition of System.IntPtr, we only do enough analysis of the signature to disambiguate the constructors we support.
            _id = IntPtrConstructorId.None;
            Method           method = methodHandle.GetMethod(reader);
            HandleCollection parameterTypeSignatureHandles = method.Signature.GetMethodSignature(reader).Parameters;

            if (parameterTypeSignatureHandles.Count == 1)
            {
                HandleCollection.Enumerator enumerator = parameterTypeSignatureHandles.GetEnumerator();
                enumerator.MoveNext();
                Handle parameterTypeHandle = enumerator.Current;

                // If any parameter is a pointer type, bail as we don't support Invokes on pointers.
                if (parameterTypeHandle.HandleType != HandleType.TypeDefinition)
                {
                    throw new PlatformNotSupportedException(SR.PlatformNotSupported_PointerArguments);
                }

                TypeDefinition typeDefinition = parameterTypeHandle.ToTypeDefinitionHandle(reader).GetTypeDefinition(reader);
                String         name           = typeDefinition.Name.GetString(reader);
                switch (name)
                {
                case "Int32":
                    _id = IntPtrConstructorId.Int32;
                    break;

                case "Int64":
                    _id = IntPtrConstructorId.Int64;
                    break;

                case "UInt32":
                    _id = IntPtrConstructorId.UInt32;
                    break;

                case "UInt64":
                    _id = IntPtrConstructorId.UInt64;
                    break;

                default:
                    break;
                }
            }
        }
Ejemplo n.º 15
0
        protected sealed override RuntimeNamedMethodInfo GetPropertyMethod(PropertyMethodSemantics whichMethod)
        {
            NativeFormatMethodSemanticsAttributes localMethodSemantics;

            switch (whichMethod)
            {
            case PropertyMethodSemantics.Getter:
                localMethodSemantics = NativeFormatMethodSemanticsAttributes.Getter;
                break;

            case PropertyMethodSemantics.Setter:
                localMethodSemantics = NativeFormatMethodSemanticsAttributes.Setter;
                break;

            default:
                return(null);
            }

            bool inherited = !_reflectedType.Equals(ContextTypeInfo);

            foreach (MethodSemanticsHandle methodSemanticsHandle in _property.MethodSemantics)
            {
                MethodSemantics methodSemantics = methodSemanticsHandle.GetMethodSemantics(_reader);
                if (methodSemantics.Attributes == localMethodSemantics)
                {
                    MethodHandle methodHandle = methodSemantics.Method;

                    if (inherited)
                    {
                        MethodAttributes flags = methodHandle.GetMethod(_reader).Flags;
                        if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private)
                        {
                            continue;
                        }
                    }

                    return(RuntimeNamedMethodInfo <NativeFormatMethodCommon> .GetRuntimeNamedMethodInfo(new NativeFormatMethodCommon(methodHandle, _definingTypeInfo, ContextTypeInfo), _reflectedType));
                }
            }

            return(null);
        }
        public StringConstructorMethodInvoker(MetadataReader reader, MethodHandle methodHandle)
        {
            // Since we control the definition of System.String, we only do enough analysis of the signature to disambiguate the constructors we support.
            _id = StringConstructorId.None;
            Method method         = methodHandle.GetMethod(reader);
            int    parameterCount = 0;

            foreach (ParameterTypeSignatureHandle parameterTypeSignatureHandle in method.Signature.GetMethodSignature(reader).Parameters)
            {
                ParameterTypeSignature parameterTypeSignature = parameterTypeSignatureHandle.GetParameterTypeSignature(reader);

                // If any parameter is a pointer type, bail as we don't support Invokes on pointers.
                if (parameterTypeSignature.Type.HandleType == HandleType.TypeSpecification)
                {
                    TypeSpecification typeSpecification = parameterTypeSignature.Type.ToTypeSpecificationHandle(reader).GetTypeSpecification(reader);
                    if (typeSpecification.Signature.HandleType == HandleType.PointerSignature)
                    {
                        return;
                    }
                }
                parameterCount++;
            }

            switch (parameterCount)
            {
            case 1:
                _id = StringConstructorId.CharArray;
                break;

            case 2:
                _id = StringConstructorId.Char_Int;
                break;

            case 3:
                _id = StringConstructorId.CharArray_Int_Int;
                break;

            default:
                break;
            }
        }
        public IntPtrConstructorMethodInvoker(MetadataReader reader, MethodHandle methodHandle)
        {
            // Since we control the definition of System.IntPtr, we only do enough analysis of the signature to disambiguate the constructors we support.
            _id = IntPtrConstructorId.None;
            Method method = methodHandle.GetMethod(reader);
            ParameterTypeSignatureHandle[] parameterTypeSignatureHandles = method.Signature.GetMethodSignature(reader).Parameters.ToArray();
            if (parameterTypeSignatureHandles.Length == 1)
            {
                ParameterTypeSignature parameterTypeSignature = parameterTypeSignatureHandles[0].GetParameterTypeSignature(reader);

                // If any parameter is a pointer type, bail as we don't support Invokes on pointers.
                if (parameterTypeSignature.Type.HandleType != HandleType.TypeDefinition)
                    throw new PlatformNotSupportedException(SR.PlatformNotSupported_PointerArguments);

                TypeDefinition typeDefinition = parameterTypeSignature.Type.ToTypeDefinitionHandle(reader).GetTypeDefinition(reader);
                String name = typeDefinition.Name.GetString(reader);
                switch (name)
                {
                    case "Int32":
                        _id = IntPtrConstructorId.Int32;
                        break;

                    case "Int64":
                        _id = IntPtrConstructorId.Int64;
                        break;

                    case "UInt32":
                        _id = IntPtrConstructorId.UInt32;
                        break;

                    case "UInt64":
                        _id = IntPtrConstructorId.UInt64;
                        break;

                    default:
                        break;
                }
            }
        }
        public StringConstructorMethodInvoker(MetadataReader reader, MethodHandle methodHandle)
        {
            // Since we control the definition of System.String, we only do enough analysis of the signature to disambiguate the constructors we support.
            _id = StringConstructorId.None;
            Method method = methodHandle.GetMethod(reader);
            int parameterCount = 0;
            foreach (ParameterTypeSignatureHandle parameterTypeSignatureHandle in method.Signature.GetMethodSignature(reader).Parameters)
            {
                ParameterTypeSignature parameterTypeSignature = parameterTypeSignatureHandle.GetParameterTypeSignature(reader);

                // If any parameter is a pointer type, bail as we don't support Invokes on pointers.
                if (parameterTypeSignature.Type.HandleType == HandleType.TypeSpecification)
                {
                    TypeSpecification typeSpecification = parameterTypeSignature.Type.ToTypeSpecificationHandle(reader).GetTypeSpecification(reader);
                    if (typeSpecification.Signature.HandleType == HandleType.PointerSignature)
                        return;
                }
                parameterCount++;
            }

            switch (parameterCount)
            {
                case 1:
                    _id = StringConstructorId.CharArray;
                    break;

                case 2:
                    _id = StringConstructorId.Char_Int;
                    break;

                case 3:
                    _id = StringConstructorId.CharArray_Int_Int;
                    break;

                default:
                    break;
            }
        }
        //
        // Used to split methods between DeclaredMethods and DeclaredConstructors.
        //
        public static bool IsConstructor(this MethodHandle methodHandle, MetadataReader reader)
        {
            Method method = methodHandle.GetMethod(reader);

            return(IsConstructor(ref method, reader));
        }
 public sealed override IEnumerable<CustomAttributeData> GetPsuedoCustomAttributes(MetadataReader reader, MethodHandle methodHandle, TypeDefinitionHandle declaringTypeHandle)
 {
     MethodImplAttributes implAttributes = methodHandle.GetMethod(reader).ImplFlags;
     if (0 != (implAttributes & MethodImplAttributes.PreserveSig))
         yield return ReflectionCoreExecution.ExecutionDomain.GetCustomAttributeData(typeof(PreserveSigAttribute), null, null);
 }