IsValidToken() публичный Метод

public IsValidToken ( int token ) : bool
token int
Результат bool
Пример #1
0
        [System.Security.SecurityCritical]  // auto-generated
        private FieldInfo ResolveLiteralField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!MetadataImport.IsValidToken(tk) || !tk.IsFieldDef)
            {
                throw new ArgumentOutOfRangeException("metadataToken",
                                                      String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Argument_InvalidToken", tk, this)));
            }

            int    tkDeclaringType;
            string fieldName;

            fieldName       = MetadataImport.GetName(tk).ToString();
            tkDeclaringType = MetadataImport.GetParentToken(tk);

            Type declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);

            declaringType.GetFields();

            try
            {
                return(declaringType.GetField(fieldName,
                                              BindingFlags.Static | BindingFlags.Instance |
                                              BindingFlags.Public | BindingFlags.NonPublic |
                                              BindingFlags.DeclaredOnly));
            }
            catch
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_ResolveField", tk, this), "metadataToken");
            }
        }
Пример #2
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override string ResolveString(int metadataToken)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!tk.IsString)
            {
                throw new ArgumentException(
                          String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Argument_ResolveString"), metadataToken, ToString()));
            }

            if (!MetadataImport.IsValidToken(tk))
            {
                throw new ArgumentOutOfRangeException("metadataToken",
                                                      String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Argument_InvalidToken", tk, this)));
            }

            string str = MetadataImport.GetUserString(metadataToken);

            if (str == null)
            {
                throw new ArgumentException(
                          String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Argument_ResolveString"), metadataToken, ToString()));
            }

            return(str);
        }
Пример #3
0
        private FieldInfo?ResolveLiteralField(int metadataToken, Type[]?genericTypeArguments, Type[]?genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!MetadataImport.IsValidToken(tk) || !tk.IsFieldDef)
            {
                throw new ArgumentOutOfRangeException(nameof(metadataToken),
                                                      SR.Format(SR.Argument_InvalidToken, tk, this));
            }

            int    tkDeclaringType;
            string fieldName;

            fieldName       = MetadataImport.GetName(tk).ToString();
            tkDeclaringType = MetadataImport.GetParentToken(tk);

            Type declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);

            declaringType.GetFields();

            try
            {
                return(declaringType.GetField(fieldName,
                                              BindingFlags.Static | BindingFlags.Instance |
                                              BindingFlags.Public | BindingFlags.NonPublic |
                                              BindingFlags.DeclaredOnly));
            }
            catch
            {
                throw new ArgumentException(SR.Format(SR.Argument_ResolveField, tk, this), nameof(metadataToken));
            }
        }
Пример #4
0
        public override string ResolveString(int metadataToken)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!tk.IsString)
            {
                throw new ArgumentException(
                          SR.Format(SR.Argument_ResolveString, metadataToken, this));
            }

            if (!MetadataImport.IsValidToken(tk))
            {
                throw new ArgumentOutOfRangeException(nameof(metadataToken),
                                                      SR.Format(SR.Argument_InvalidToken, tk, this));
            }

            string?str = MetadataImport.GetUserString(metadataToken);

            if (str == null)
            {
                throw new ArgumentException(
                          SR.Format(SR.Argument_ResolveString, metadataToken, this));
            }

            return(str);
        }
Пример #5
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!MetadataImport.IsValidToken(tk))
            {
                throw new ArgumentOutOfRangeException("metadataToken",
                                                      Environment.GetResourceString("Argument_InvalidToken", tk, this));
            }

            RuntimeTypeHandle[] typeArgs   = ConvertToTypeHandleArray(genericTypeArguments);
            RuntimeTypeHandle[] methodArgs = ConvertToTypeHandleArray(genericMethodArguments);

            try
            {
                IRuntimeFieldInfo fieldHandle = null;

                if (!tk.IsFieldDef)
                {
                    if (!tk.IsMemberRef)
                    {
                        throw new ArgumentException("metadataToken",
                                                    Environment.GetResourceString("Argument_ResolveField", tk, this));
                    }

                    unsafe
                    {
                        ConstArray sig = MetadataImport.GetMemberRefProps(tk);

                        if (*(MdSigCallingConvention *)sig.Signature.ToPointer() != MdSigCallingConvention.Field)
                        {
                            throw new ArgumentException("metadataToken",
                                                        Environment.GetResourceString("Argument_ResolveField", tk, this));
                        }
                    }

                    fieldHandle = ModuleHandle.ResolveFieldHandleInternal(GetNativeHandle(), tk, typeArgs, methodArgs);
                }

                fieldHandle = ModuleHandle.ResolveFieldHandleInternal(GetNativeHandle(), metadataToken, typeArgs, methodArgs);
                RuntimeType declaringType = RuntimeFieldHandle.GetApproxDeclaringType(fieldHandle.Value);

                if (declaringType.IsGenericType || declaringType.IsArray)
                {
                    int tkDeclaringType = ModuleHandle.GetMetadataImport(GetNativeHandle()).GetParentToken(metadataToken);
                    declaringType = (RuntimeType)ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
                }

                return(System.RuntimeType.GetFieldInfo(declaringType, fieldHandle));
            }
            catch (MissingFieldException)
            {
                return(ResolveLiteralField(tk, genericTypeArguments, genericMethodArguments));
            }
            catch (BadImageFormatException e)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), e);
            }
        }
Пример #6
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!MetadataImport.IsValidToken(tk))
            {
                throw new ArgumentOutOfRangeException("metadataToken",
                                                      Environment.GetResourceString("Argument_InvalidToken", tk, this));
            }

            RuntimeTypeHandle[] typeArgs   = ConvertToTypeHandleArray(genericTypeArguments);
            RuntimeTypeHandle[] methodArgs = ConvertToTypeHandleArray(genericMethodArguments);

            try
            {
                if (!tk.IsMethodDef && !tk.IsMethodSpec)
                {
                    if (!tk.IsMemberRef)
                    {
                        throw new ArgumentException("metadataToken",
                                                    Environment.GetResourceString("Argument_ResolveMethod", tk, this));
                    }

                    unsafe
                    {
                        ConstArray sig = MetadataImport.GetMemberRefProps(tk);

                        if (*(MdSigCallingConvention *)sig.Signature.ToPointer() == MdSigCallingConvention.Field)
                        {
                            throw new ArgumentException("metadataToken",
                                                        Environment.GetResourceString("Argument_ResolveMethod", tk, this));
                        }
                    }
                }

                IRuntimeMethodInfo methodHandle = ModuleHandle.ResolveMethodHandleInternal(GetNativeHandle(), tk, typeArgs, methodArgs);
                Type declaringType = RuntimeMethodHandle.GetDeclaringType(methodHandle);

                if (declaringType.IsGenericType || declaringType.IsArray)
                {
                    MetadataToken tkDeclaringType = new MetadataToken(MetadataImport.GetParentToken(tk));

                    if (tk.IsMethodSpec)
                    {
                        tkDeclaringType = new MetadataToken(MetadataImport.GetParentToken(tkDeclaringType));
                    }

                    declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
                }

                return(System.RuntimeType.GetMethodBase(declaringType as RuntimeType, methodHandle));
            }
            catch (BadImageFormatException e)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), e);
            }
        }
Пример #7
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (tk.IsProperty)
            {
                throw new ArgumentException(Environment.GetResourceString("InvalidOperation_PropertyInfoNotAvailable"));
            }

            if (tk.IsEvent)
            {
                throw new ArgumentException(Environment.GetResourceString("InvalidOperation_EventInfoNotAvailable"));
            }

            if (tk.IsMethodSpec || tk.IsMethodDef)
            {
                return(ResolveMethod(metadataToken, genericTypeArguments, genericMethodArguments));
            }

            if (tk.IsFieldDef)
            {
                return(ResolveField(metadataToken, genericTypeArguments, genericMethodArguments));
            }

            if (tk.IsTypeRef || tk.IsTypeDef || tk.IsTypeSpec)
            {
                return(ResolveType(metadataToken, genericTypeArguments, genericMethodArguments));
            }

            if (tk.IsMemberRef)
            {
                if (!MetadataImport.IsValidToken(tk))
                {
                    throw new ArgumentOutOfRangeException("metadataToken",
                                                          Environment.GetResourceString("Argument_InvalidToken", tk, this));
                }

                ConstArray sig = MetadataImport.GetMemberRefProps(tk);

                unsafe
                {
                    if (*(MdSigCallingConvention *)sig.Signature.ToPointer() == MdSigCallingConvention.Field)
                    {
                        return(ResolveField(tk, genericTypeArguments, genericMethodArguments));
                    }
                    else
                    {
                        return(ResolveMethod(tk, genericTypeArguments, genericMethodArguments));
                    }
                }
            }

            throw new ArgumentException("metadataToken",
                                        Environment.GetResourceString("Argument_ResolveMember", tk, this));
        }
Пример #8
0
        public override MemberInfo?ResolveMember(int metadataToken, Type[]?genericTypeArguments, Type[]?genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (tk.IsProperty)
            {
                throw new ArgumentException(SR.InvalidOperation_PropertyInfoNotAvailable);
            }

            if (tk.IsEvent)
            {
                throw new ArgumentException(SR.InvalidOperation_EventInfoNotAvailable);
            }

            if (tk.IsMethodSpec || tk.IsMethodDef)
            {
                return(ResolveMethod(metadataToken, genericTypeArguments, genericMethodArguments));
            }

            if (tk.IsFieldDef)
            {
                return(ResolveField(metadataToken, genericTypeArguments, genericMethodArguments));
            }

            if (tk.IsTypeRef || tk.IsTypeDef || tk.IsTypeSpec)
            {
                return(ResolveType(metadataToken, genericTypeArguments, genericMethodArguments));
            }

            if (tk.IsMemberRef)
            {
                if (!MetadataImport.IsValidToken(tk))
                {
                    throw new ArgumentOutOfRangeException(nameof(metadataToken),
                                                          SR.Format(SR.Argument_InvalidToken, tk, this));
                }

                ConstArray sig = MetadataImport.GetMemberRefProps(tk);

                unsafe
                {
                    if (*(MdSigCallingConvention *)sig.Signature == MdSigCallingConvention.Field)
                    {
                        return(ResolveField(tk, genericTypeArguments, genericMethodArguments));
                    }
                    else
                    {
                        return(ResolveMethod(tk, genericTypeArguments, genericMethodArguments));
                    }
                }
            }

            throw new ArgumentException(SR.Format(SR.Argument_ResolveMember, tk, this),
                                        nameof(metadataToken));
        }
Пример #9
0
        public override byte[] ResolveSignature(int metadataToken)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!MetadataImport.IsValidToken(tk))
            {
                throw new ArgumentOutOfRangeException(
                          nameof(metadataToken),
                          SR.Format(SR.Argument_InvalidToken, tk, this)
                          );
            }

            if (
                !tk.IsMemberRef &&
                !tk.IsMethodDef &&
                !tk.IsTypeSpec &&
                !tk.IsSignature &&
                !tk.IsFieldDef
                )
            {
                throw new ArgumentException(
                          SR.Format(SR.Argument_InvalidToken, tk, this),
                          nameof(metadataToken)
                          );
            }

            ConstArray signature;

            if (tk.IsMemberRef)
            {
                signature = MetadataImport.GetMemberRefProps(metadataToken);
            }
            else
            {
                signature = MetadataImport.GetSignatureFromToken(metadataToken);
            }

            byte[] sig = new byte[signature.Length];

            for (int i = 0; i < signature.Length; i++)
            {
                sig[i] = signature[i];
            }

            return(sig);
        }
Пример #10
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (tk.IsGlobalTypeDefToken)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_ResolveModuleType", tk), "metadataToken");
            }

            if (!MetadataImport.IsValidToken(tk))
            {
                throw new ArgumentOutOfRangeException("metadataToken",
                                                      Environment.GetResourceString("Argument_InvalidToken", tk, this));
            }

            if (!tk.IsTypeDef && !tk.IsTypeSpec && !tk.IsTypeRef)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_ResolveType", tk, this), "metadataToken");
            }

            RuntimeTypeHandle[] typeArgs   = ConvertToTypeHandleArray(genericTypeArguments);
            RuntimeTypeHandle[] methodArgs = ConvertToTypeHandleArray(genericMethodArguments);

            try
            {
                Type t = GetModuleHandle().ResolveTypeHandle(metadataToken, typeArgs, methodArgs).GetRuntimeType();

                if (t == null)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_ResolveType", tk, this), "metadataToken");
                }

                return(t);
            }
            catch (BadImageFormatException e)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), e);
            }
        }
Пример #11
0
        public override Type ResolveType(int metadataToken, Type[]?genericTypeArguments, Type[]?genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (tk.IsGlobalTypeDefToken)
            {
                throw new ArgumentException(SR.Format(SR.Argument_ResolveModuleType, tk), nameof(metadataToken));
            }

            if (!MetadataImport.IsValidToken(tk))
            {
                throw new ArgumentOutOfRangeException(nameof(metadataToken),
                                                      SR.Format(SR.Argument_InvalidToken, tk, this));
            }

            if (!tk.IsTypeDef && !tk.IsTypeSpec && !tk.IsTypeRef)
            {
                throw new ArgumentException(SR.Format(SR.Argument_ResolveType, tk, this), nameof(metadataToken));
            }

            RuntimeTypeHandle[]? typeArgs   = ConvertToTypeHandleArray(genericTypeArguments);
            RuntimeTypeHandle[]? methodArgs = ConvertToTypeHandleArray(genericMethodArguments);

            try
            {
                Type t = GetModuleHandleImpl().ResolveTypeHandle(metadataToken, typeArgs, methodArgs).GetRuntimeType();

                if (t == null)
                {
                    throw new ArgumentException(SR.Format(SR.Argument_ResolveType, tk, this), nameof(metadataToken));
                }

                return(t);
            }
            catch (BadImageFormatException e)
            {
                throw new ArgumentException(SR.Argument_BadImageFormatExceptionResolve, e);
            }
        }
Пример #12
0
        public byte[] ResolveSignature(int metadataToken)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!MetadataImport.IsValidToken(tk))
            {
                throw new ArgumentOutOfRangeException("metadataToken",
                                                      String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidToken", tk, this)));
            }

            if (!tk.IsMemberRef && !tk.IsMethodDef && !tk.IsTypeSpec && !tk.IsSignature && !tk.IsFieldDef)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidToken", tk, this)),
                                            "metadataToken");
            }

            ConstArray signature;

            if (tk.IsMemberRef)
            {
                signature = MetadataImport.GetMemberRefProps(metadataToken);
            }
            else
            {
                signature = MetadataImport.GetSignatureFromToken(metadataToken);
            }

            byte[] sig = new byte[signature.Length];

            for (int i = 0; i < signature.Length; i++)
            {
                sig[i] = signature[i];
            }

            return(sig);
        }
Пример #13
0
        public override FieldInfo?ResolveField(int metadataToken, Type[]?genericTypeArguments, Type[]?genericMethodArguments)
        {
            try
            {
                MetadataToken tk = new MetadataToken(metadataToken);

                if (!MetadataImport.IsValidToken(tk))
                {
                    throw new ArgumentOutOfRangeException(nameof(metadataToken),
                                                          SR.Format(SR.Argument_InvalidToken, tk, this));
                }

                RuntimeTypeHandle[]? typeArgs   = null;
                RuntimeTypeHandle[]? methodArgs = null;
                if (genericTypeArguments?.Length > 0)
                {
                    typeArgs = ConvertToTypeHandleArray(genericTypeArguments);
                }
                if (genericMethodArguments?.Length > 0)
                {
                    methodArgs = ConvertToTypeHandleArray(genericMethodArguments);
                }

                ModuleHandle moduleHandle = new ModuleHandle(this);
                if (!tk.IsFieldDef)
                {
                    if (!tk.IsMemberRef)
                    {
                        throw new ArgumentException(SR.Format(SR.Argument_ResolveField, tk, this),
                                                    nameof(metadataToken));
                    }

                    unsafe
                    {
                        ConstArray sig = MetadataImport.GetMemberRefProps(tk);

                        if (*(MdSigCallingConvention *)sig.Signature != MdSigCallingConvention.Field)
                        {
                            throw new ArgumentException(SR.Format(SR.Argument_ResolveField, tk, this),
                                                        nameof(metadataToken));
                        }
                    }
                }

                IRuntimeFieldInfo fieldHandle = moduleHandle.ResolveFieldHandle(metadataToken, typeArgs, methodArgs).GetRuntimeFieldInfo();

                RuntimeType declaringType = RuntimeFieldHandle.GetApproxDeclaringType(fieldHandle.Value);

                if (declaringType.IsGenericType || declaringType.IsArray)
                {
                    int tkDeclaringType = ModuleHandle.GetMetadataImport(this).GetParentToken(metadataToken);
                    declaringType = (RuntimeType)ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
                }

                return(RuntimeType.GetFieldInfo(declaringType, fieldHandle));
            }
            catch (MissingFieldException)
            {
                return(ResolveLiteralField(metadataToken, genericTypeArguments, genericMethodArguments));
            }
            catch (BadImageFormatException e)
            {
                throw new ArgumentException(SR.Argument_BadImageFormatExceptionResolve, e);
            }
        }