Example #1
0
        internal MethodDefinition(MetadataImage image, MetadataRow <FileSegment, MethodImplAttributes, MethodAttributes, uint, uint, uint> row)
            : base(row.MetadataToken)
        {
            _image = image;
            var stringStream = image.Header.GetStream <StringStream>();
            var blobStream   = image.Header.GetStream <BlobStream>();

            ImplAttributes = row.Column2;
            Attributes     = row.Column3;
            _name          = new LazyValue <string>(() => stringStream.GetStringByOffset(row.Column4));

            IBinaryStreamReader blobReader;

            if (blobStream.TryCreateBlobReader(row.Column5, out blobReader))
            {
                _signature = new LazyValue <MethodSignature>(() => MethodSignature.FromReader(image, blobReader));
            }

            _methodBody = new LazyValue <MethodBody>(() =>
            {
                var rawBody = row.Column1 as CilRawMethodBody;
                if (rawBody != null)
                {
                    return(CilMethodBody.FromRawMethodBody(this, rawBody));
                }

                // TODO: handler for native methods.
                return(null);
            });

            _declaringType = new LazyValue <TypeDefinition>(() =>
            {
                var table   = image.Header.GetStream <TableStream>().GetTable(MetadataTokenType.TypeDef);
                var typeRow = table.GetRowClosestToKey(5, row.MetadataToken.Rid);
                return((TypeDefinition)table.GetMemberFromRow(image, typeRow));
            });

            _pinvokeMap = new LazyValue <ImplementationMap>(() =>
            {
                if (!row.Column3.HasFlag(MethodAttributes.PInvokeImpl))
                {
                    return(null);
                }

                var table  = (ImplementationMapTable)image.Header.GetStream <TableStream>().GetTable(MetadataTokenType.ImplMap);
                var mapRow = table.FindImplementationMapOfOwner(row.MetadataToken);
                return(mapRow != null ? (ImplementationMap)table.GetMemberFromRow(image, mapRow) : null);
            });

            Parameters = new RangedMemberCollection <MethodDefinition, ParameterDefinition>(this, MetadataTokenType.Param, 5, GetParamOwner, SetParamOwner);

            CustomAttributes     = new CustomAttributeCollection(this);
            SecurityDeclarations = new SecurityDeclarationCollection(this);
            GenericParameters    = new GenericParameterCollection(this);
        }
Example #2
0
        internal PropertyMap(MetadataImage image, MetadataRow <uint, uint> row)
            : base(row.MetadataToken)
        {
            _image  = image;
            _parent = new LazyValue <TypeDefinition>(() =>
            {
                var typeTable = image.Header.GetStream <TableStream>().GetTable(MetadataTokenType.TypeDef);
                return((TypeDefinition)typeTable.GetMemberFromRow(image, typeTable.GetRow((int)(row.Column1 - 1))));
            });

            Properties = new RangedMemberCollection <PropertyMap, PropertyDefinition>(this, MetadataTokenType.Property,
                                                                                      1, GetPropertyOwner, SetPropertyOwner);
        }
Example #3
0
        internal EventMap(MetadataImage image, MetadataRow <uint, uint> row)
            : base(row.MetadataToken)
        {
            _image  = image;
            _parent = new LazyValue <TypeDefinition>(() =>
            {
                var typeTable = image.Header.GetStream <TableStream>().GetTable(MetadataTokenType.TypeDef);
                return(typeTable.TryGetRow((int)row.Column1 - 1, out var typeRow)
                    ? (TypeDefinition)typeTable.GetMemberFromRow(image, typeRow)
                    : null);
            });

            Events = new RangedMemberCollection <EventMap, EventDefinition>(this, MetadataTokenType.Event, 1, GetEventOwner, SetEventOwner);
        }
Example #4
0
        internal TypeDefinition(MetadataImage image, MetadataRow <TypeAttributes, uint, uint, uint, uint, uint> row)
            : base(row.MetadataToken)
        {
            Module = image.Assembly.Modules.FirstOrDefault();
            var tableStream  = image.Header.GetStream <TableStream>();
            var stringStream = image.Header.GetStream <StringStream>();

            Attributes = row.Column1;

            _name      = _namespace = new LazyValue <string>(() => stringStream.GetStringByOffset(row.Column2));
            _namespace = new LazyValue <string>(() => stringStream.GetStringByOffset(row.Column3));
            _baseType  = new LazyValue <ITypeDefOrRef>(() =>
            {
                var baseTypeToken = tableStream.GetIndexEncoder(CodedIndex.TypeDefOrRef).DecodeIndex(row.Column4);
                if (baseTypeToken.Rid != 0)
                {
                    IMetadataMember baseType;
                    if (image.TryResolveMember(baseTypeToken, out baseType))
                    {
                        return(baseType as ITypeDefOrRef);
                    }
                }
                return(null);
            });

            Fields  = new RangedMemberCollection <TypeDefinition, FieldDefinition>(this, MetadataTokenType.Field, 4, GetFieldOwner, SetFieldOwner);
            Methods = new RangedMemberCollection <TypeDefinition, MethodDefinition>(this, MetadataTokenType.Method, 5, GetMethodOwner, SetMethodOwner);

            _classLayout = new LazyValue <ClassLayout>(() =>
            {
                var table     = image.Header.GetStream <TableStream>().GetTable(MetadataTokenType.ClassLayout);
                var layoutRow = table.GetRowByKey(2, row.MetadataToken.Rid);
                return(layoutRow != null ? (ClassLayout)table.GetMemberFromRow(image, layoutRow) : null);
            });

            _propertyMap = new LazyValue <PropertyMap>(() =>
            {
                var table  = image.Header.GetStream <TableStream>().GetTable(MetadataTokenType.PropertyMap);
                var mapRow = table.GetRowByKey(0, row.MetadataToken.Rid);
                return(mapRow != null ? (PropertyMap)table.GetMemberFromRow(image, mapRow) : null);
            });

            _eventMap = new LazyValue <EventMap>(() =>
            {
                var table  = image.Header.GetStream <TableStream>().GetTable(MetadataTokenType.EventMap);
                var mapRow = table.GetRowByKey(0, row.MetadataToken.Rid);
                return(mapRow != null ? (EventMap)table.GetMemberFromRow(image, mapRow) : null);
            });

            _declaringType = new LazyValue <TypeDefinition>(() =>
            {
                var table          = image.Header.GetStream <TableStream>().GetTable(MetadataTokenType.NestedClass);
                var nestedClassRow = table.GetRowByKey(0, row.MetadataToken.Rid);
                return(nestedClassRow != null
                    ? ((NestedClass)table.GetMemberFromRow(image, nestedClassRow)).EnclosingClass
                    : null);
            });

            CustomAttributes      = new CustomAttributeCollection(this);
            SecurityDeclarations  = new SecurityDeclarationCollection(this);
            NestedClasses         = new NestedClassCollection(this);
            GenericParameters     = new GenericParameterCollection(this);
            Interfaces            = new InterfaceImplementationCollection(this);
            MethodImplementations = new MethodImplementationCollection(this);
        }