Beispiel #1
0
        /// <summary>
        /// Attempts to resolve the type.
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        internal TypeRef ResolveType(AssemblyDef assembly, ReflectedMember member)
        {
            TypeRef type = null;

            if (ElementType.ElementType == ElementTypes.SZArray)
            {
                TypeSignatureToken childType = (TypeSignatureToken)Tokens.Last();
                type = childType.ResolveType(assembly, member);
            }
            else if (
                ElementType.ElementType == ElementTypes.Class ||
                ElementType.ElementType == ElementTypes.ValueType
                )
            {
                type = (TypeRef)assembly.ResolveMetadataToken(ElementType.Token);
            }
            else if (ElementType.ElementType == ElementTypes.GenericInstance)
            {
                ElementTypeSignatureToken childType = (ElementTypeSignatureToken)Tokens[1];
                type = childType.ResolveToken(assembly);
            }
            else if (ElementType.ElementType == ElementTypes.Ptr)
            {
                if (Tokens[1].TokenType == SignatureTokens.Type)
                {
                    TypeSignatureToken childType = (TypeSignatureToken)Tokens[1];
                    type = childType.ResolveType(assembly, member);
                }
                else
                {
                    ElementTypeSignatureToken childType = (ElementTypeSignatureToken)Tokens[1];
                    type = childType.ResolveToken(assembly);
                }
            }
            else if (ElementType.ElementType == ElementTypes.Array)
            {
                if (Tokens[1].TokenType == SignatureTokens.Type)
                {
                    TypeSignatureToken childType = (TypeSignatureToken)Tokens[1];
                    type = childType.ResolveType(assembly, member);
                }
                else
                {
                    ElementTypeSignatureToken childType = (ElementTypeSignatureToken)Tokens[1];
                    type = childType.ResolveToken(assembly);
                }
            }
            else if (ElementType.ElementType == ElementTypes.MVar)
            {
                MethodDef method = member as MethodDef;
                if (method == null)
                {
                    InvalidOperationException ex = new InvalidOperationException(
                        string.Format(
                            "A MethodDef was expected for type resolution in the signiture but a {0} was provided.",
                            member.GetType().ToString()
                            ));
                    throw ex;
                }
                type = method.GenericTypes[(int)ElementType.Token];
            }
            else if (ElementType.ElementType == ElementTypes.Var)
            {
                // Anything that contains a Type property will do here
                TypeDef theType;
                if (member is MethodDef)
                {
                    // a method may define its own parameters
                    theType = (TypeDef)((MethodDef)member).Type;
                }
                else if (member is PropertyDef)
                {
                    theType = ((PropertyDef)member).OwningType;
                }
                else if (member is FieldDef)
                {
                    theType = (TypeDef)((FieldDef)member).Type;
                }
                else if (member is EventDef)
                {
                    theType = ((EventDef)member).Type;
                }
                else if (member is TypeSpec)
                {
                    theType = ((TypeSpec)member).ImplementingType;
                }
                else
                {
                    InvalidOperationException ex = new InvalidOperationException(
                        string.Format(
                            "A ReflectedMember that is contained by a TypeDef was expected for type resolution in the signiture but a {0} was provided.",
                            member.GetType().ToString()
                            ));
                    throw ex;
                }

                List <GenericTypeRef> genericParameters = theType.GenericTypes;

                if (genericParameters.Count < ElementType.Token)
                {
                    throw new InvalidOperationException("The generic token refers to a parameter that is not available.");
                }
                type = genericParameters[(int)ElementType.Token];
            }
            else if (ElementType.Definition != null)
            {
                type = (TypeRef)ElementType.Definition;
            }
            return(type);
        }
        /// <summary>
        /// Attempts to resolve the type.
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        internal TypeRef ResolveType(AssemblyDef assembly, ParamDef parameter)
        {
            TypeRef type = null;

            if (ElementType.ElementType == ElementTypes.SZArray)
            {
                TypeSignatureToken childType = Tokens.Last() as TypeSignatureToken;
                type = childType.ResolveType(assembly, parameter);
            }
            else if (
                ElementType.ElementType == ElementTypes.Class ||
                ElementType.ElementType == ElementTypes.ValueType
                )
            {
                type = (TypeRef)assembly.ResolveMetadataToken(ElementType.Token);
            }
            else if (ElementType.ElementType == ElementTypes.GenericInstance)
            {
                ElementTypeSignatureToken childType = Tokens[1] as ElementTypeSignatureToken;
                type = childType.ResolveToken(assembly);
            }
            else if (ElementType.ElementType == ElementTypes.Ptr)
            {
                if (Tokens[1].TokenType == SignatureTokens.Type)
                {
                    TypeSignatureToken childType = Tokens[1] as TypeSignatureToken;
                    type = childType.ResolveType(assembly, parameter);
                }
                else
                {
                    ElementTypeSignatureToken childType = Tokens[1] as ElementTypeSignatureToken;
                    type = childType.ResolveToken(assembly);
                }
            }
            else if (ElementType.ElementType == ElementTypes.Array)
            {
                if (Tokens[1].TokenType == SignatureTokens.Type)
                {
                    TypeSignatureToken childType = Tokens[1] as TypeSignatureToken;
                    type = childType.ResolveType(assembly, parameter);
                }
                else
                {
                    ElementTypeSignatureToken childType = (ElementTypeSignatureToken)this.Tokens[1];
                    type = childType.ResolveToken(assembly);
                }
            }
            else if (ElementType.ElementType == ElementTypes.MVar)
            {
                type = parameter.Method.GenericTypes[(int)ElementType.Token];
            }
            else if (ElementType.ElementType == ElementTypes.Var)
            {
                List <GenericTypeRef> genericParameters = ((TypeDef)parameter.Method.Type).GenericTypes;
                if (genericParameters.Count < ElementType.Token)
                {
                    throw new InvalidOperationException("The generic token refers to a parameter that is not available.");
                }
                type = genericParameters[(int)ElementType.Token];
            }
            else if (ElementType.Definition != null)
            {
                type = ElementType.Definition as TypeRef;
            }
            return(type);
        }