Exemplo n.º 1
0
        public bool IsDerivedFrom(TypeReference type, TypeReference baseType)
        {
            if (baseType.MetadataType == MetadataType.Array)
            {
                throw new NotImplementedException();
            }
            Mono.Cecil.TokenType tokenType = type.MetadataToken.TokenType;
            if (tokenType != Mono.Cecil.TokenType.TypeRef)
            {
                if (tokenType != Mono.Cecil.TokenType.TypeDef)
                {
                    if (tokenType != Mono.Cecil.TokenType.TypeSpec)
                    {
                        throw new NotSupportedException(string.Format("Metadata token type {0} is not supported.", type.MetadataToken.TokenType));
                    }
                    MetadataType metadataType = type.MetadataType;
                    if (metadataType != MetadataType.Array)
                    {
                        if (metadataType != MetadataType.GenericInstance)
                        {
                            throw new NotSupportedException(string.Format("Metadata type {0} is not supported.", type.MetadataType));
                        }
                    }
                    else
                    {
                        return(false);
                    }
                    GenericInstanceType type4 = (GenericInstanceType)type;
                    return(this.IsDerivedFrom(type4.ElementType, baseType));
                }
            }
            else
            {
                return(this.IsDerivedFrom(ResolveType(type), baseType));
            }
            TypeDefinition definition = (TypeDefinition)type;

            if ((definition.FullName == baseType.FullName) && (definition.Scope.GetAssemblyName() == ResolveType(baseType).Scope.GetAssemblyName()))
            {
                return(true);
            }
            if (definition.BaseType == null)
            {
                return(false);
            }
            return(this.IsDerivedFrom(definition.BaseType, baseType));
        }
Exemplo n.º 2
0
        private static void ParseCode(int code_size, ILParser.ParseContext context)
        {
            CodeReader     code     = context.Code;
            MetadataReader metadata = context.Metadata;
            IILVisitor     visitor  = context.Visitor;
            int            codeSize = code.position + code_size;

            while (code.position < codeSize)
            {
                byte   num    = code.ReadByte();
                OpCode opCode = (num != 254 ? OpCodes.OneByteOpCode[num] : OpCodes.TwoBytesOpCode[code.ReadByte()]);
                switch (opCode.OperandType)
                {
                case OperandType.InlineBrTarget:
                {
                    visitor.OnInlineBranch(opCode, code.ReadInt32());
                    continue;
                }

                case OperandType.InlineField:
                case OperandType.InlineMethod:
                case OperandType.InlineTok:
                case OperandType.InlineType:
                {
                    IMetadataTokenProvider metadataTokenProvider = metadata.LookupToken(code.ReadToken());
                    Mono.Cecil.TokenType   tokenType             = metadataTokenProvider.MetadataToken.TokenType;
                    if (tokenType > Mono.Cecil.TokenType.Field)
                    {
                        if (tokenType > Mono.Cecil.TokenType.MemberRef)
                        {
                            if (tokenType == Mono.Cecil.TokenType.TypeSpec)
                            {
                                goto Label0;
                            }
                            if (tokenType != Mono.Cecil.TokenType.MethodSpec)
                            {
                                continue;
                            }
                        }
                        else if (tokenType != Mono.Cecil.TokenType.Method)
                        {
                            if (tokenType == Mono.Cecil.TokenType.MemberRef)
                            {
                                FieldReference fieldReference = metadataTokenProvider as FieldReference;
                                if (fieldReference == null)
                                {
                                    MethodReference methodReference = metadataTokenProvider as MethodReference;
                                    if (methodReference == null)
                                    {
                                        throw new InvalidOperationException();
                                    }
                                    visitor.OnInlineMethod(opCode, methodReference);
                                    continue;
                                }
                                else
                                {
                                    visitor.OnInlineField(opCode, fieldReference);
                                    continue;
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                        visitor.OnInlineMethod(opCode, (MethodReference)metadataTokenProvider);
                        continue;
                    }
                    else if (tokenType != Mono.Cecil.TokenType.TypeRef && tokenType != Mono.Cecil.TokenType.TypeDef)
                    {
                        if (tokenType == Mono.Cecil.TokenType.Field)
                        {
                            visitor.OnInlineField(opCode, (FieldReference)metadataTokenProvider);
                            continue;
                        }
                        else
                        {
                            continue;
                        }
                    }
Label0:
                    visitor.OnInlineType(opCode, (TypeReference)metadataTokenProvider);
                    continue;
                }

                case OperandType.InlineI:
                {
                    visitor.OnInlineInt32(opCode, code.ReadInt32());
                    continue;
                }

                case OperandType.InlineI8:
                {
                    visitor.OnInlineInt64(opCode, code.ReadInt64());
                    continue;
                }

                case OperandType.InlineNone:
                {
                    visitor.OnInlineNone(opCode);
                    continue;
                }

                case OperandType.InlineR:
                {
                    visitor.OnInlineDouble(opCode, code.ReadDouble());
                    continue;
                }

                case OperandType.InlineSig:
                {
                    visitor.OnInlineSignature(opCode, code.GetCallSite(code.ReadToken()));
                    continue;
                }

                case OperandType.InlineString:
                {
                    visitor.OnInlineString(opCode, code.GetString(code.ReadToken()));
                    continue;
                }

                case OperandType.InlineSwitch:
                {
                    int   num1     = code.ReadInt32();
                    int[] numArray = new int[num1];
                    for (int i = 0; i < num1; i++)
                    {
                        numArray[i] = code.ReadInt32();
                    }
                    visitor.OnInlineSwitch(opCode, numArray);
                    continue;
                }

                case OperandType.InlineVar:
                {
                    visitor.OnInlineVariable(opCode, ILParser.GetVariable(context, code.ReadInt16()));
                    continue;
                }

                case OperandType.InlineArg:
                {
                    visitor.OnInlineArgument(opCode, code.GetParameter(code.ReadInt16()));
                    continue;
                }

                case OperandType.ShortInlineBrTarget:
                {
                    visitor.OnInlineBranch(opCode, code.ReadSByte());
                    continue;
                }

                case OperandType.ShortInlineI:
                {
                    if (opCode != OpCodes.Ldc_I4_S)
                    {
                        visitor.OnInlineByte(opCode, code.ReadByte());
                        continue;
                    }
                    else
                    {
                        visitor.OnInlineSByte(opCode, code.ReadSByte());
                        continue;
                    }
                }

                case OperandType.ShortInlineR:
                {
                    visitor.OnInlineSingle(opCode, code.ReadSingle());
                    continue;
                }

                case OperandType.ShortInlineVar:
                {
                    visitor.OnInlineVariable(opCode, ILParser.GetVariable(context, (int)code.ReadByte()));
                    continue;
                }

                case OperandType.ShortInlineArg:
                {
                    visitor.OnInlineArgument(opCode, code.GetParameter((int)code.ReadByte()));
                    continue;
                }

                default:
                {
                    continue;
                }
                }
            }
        }
        public TypeWrapper AddType(TypeReference type)
        {
            TypeWrapper wrapper;

            if (!this.types.TryGetType(type.FullName, out wrapper))
            {
                Mono.Cecil.TokenType tokenType = type.MetadataToken.TokenType;
                if (tokenType != Mono.Cecil.TokenType.TypeRef)
                {
                    if (tokenType != Mono.Cecil.TokenType.TypeDef)
                    {
                        if (tokenType != Mono.Cecil.TokenType.TypeSpec)
                        {
                            throw new NotSupportedException(string.Format("Metadata token type {0} is not supported.", type.MetadataToken.TokenType));
                        }
                        MetadataType metadataType = type.MetadataType;
                        if (metadataType != MetadataType.Array)
                        {
                            if (metadataType != MetadataType.GenericInstance)
                            {
                                throw new NotSupportedException(string.Format("Metadata type {0} is not supported.", type.MetadataType));
                            }
                        }
                        else
                        {
                            ArrayType type4 = (ArrayType)type;
                            wrapper = new TypeWrapper(this, type4);
                            this.types.Add(type.FullName, wrapper);
                            wrapper.BaseType = this.metadataContainer.AddType(this.metadataContainer.OperationContext.ArrayType);
                            return(wrapper);
                        }
                        GenericInstanceType type5 = (GenericInstanceType)type;
                        wrapper = new TypeWrapper(this, type5);
                        this.types.Add(type.FullName, wrapper);
                        TypeDefinition definition2 = type5.ElementType.Resolve();
                        TypeReference  baseType    = definition2.BaseType;
                        if (baseType != null)
                        {
                            if (baseType.IsGenericInstance)
                            {
                                GenericInstanceType type6 = (GenericInstanceType)baseType;
                                baseType = MetadataContainer.MakeGenericType(type5, type6);
                            }
                            wrapper.BaseType = this.metadataContainer.AddType(baseType);
                        }
                        if (definition2.HasInterfaces)
                        {
                            TypeWrapper[] wrapperArray2 = new TypeWrapper[definition2.Interfaces.Count];
                            for (int i = 0; i < wrapperArray2.Length; i++)
                            {
                                TypeReference reference2 = definition2.Interfaces[i];
                                if (reference2.IsGenericInstance)
                                {
                                    GenericInstanceType type7 = (GenericInstanceType)reference2;
                                    reference2 = MetadataContainer.MakeGenericType(type5, type7);
                                }
                                wrapperArray2[i] = this.metadataContainer.AddType(reference2);
                            }
                            wrapper.Interfaces = wrapperArray2;
                        }
                        wrapper.Methods = this.GetCallableMethods(wrapper, definition2, this.system);
                        return(wrapper);
                    }
                }
                else
                {
                    return(this.metadataContainer.GetAssembly(type.Resolve().Scope).AddType(type.Resolve()));
                }
                TypeDefinition definition = (TypeDefinition)type;
                wrapper = new TypeWrapper(this, definition);
                this.types.Add(type.FullName, wrapper);
                if (definition.BaseType != null)
                {
                    wrapper.BaseType = this.metadataContainer.AddType(definition.BaseType);
                }
                if (definition.DeclaringType != null)
                {
                    wrapper.DeclaringType = this.metadataContainer.AddType(definition.DeclaringType);
                }
                if (definition.HasInterfaces)
                {
                    TypeWrapper[] wrapperArray = new TypeWrapper[definition.Interfaces.Count];
                    for (int j = 0; j < wrapperArray.Length; j++)
                    {
                        wrapperArray[j] = this.metadataContainer.AddType(definition.Interfaces[j]);
                    }
                    wrapper.Interfaces = wrapperArray;
                }
                wrapper.Methods = this.GetCallableMethods(wrapper, definition, this.system);
                if (!this.system)
                {
                    wrapper.Fields = this.GetFields(definition);
                }
            }
            return(wrapper);
        }