Exemple #1
0
        private void ReserveTypes(IEnumerable <Type> types)
        {
            var offset = new TypeDefinitionMetadataOffset
            {
                FieldIndex    = _metadata.Builder.GetRowCount(TableIndex.Field),
                PropertyIndex = _metadata.Builder.GetRowCount(TableIndex.PropertyMap),
                MethodIndex   = _metadata.Builder.GetRowCount(TableIndex.MethodDef),
                EventIndex    = _metadata.Builder.GetRowCount(TableIndex.EventMap)
            };

            foreach (var type in types)
            {
                var nextOffset = ReserveTypeDefinition(type, offset);
                offset = nextOffset;
            }
        }
        private void ReserveType(Type type, ref TypeDefinitionMetadataOffset offset)
        {
            var typeHandle = MetadataTokens.TypeDefinitionHandle(++offset.TypeIndex);

            _metadata.ReserveTypeDefinition(type, typeHandle);

            foreach (var field in type.GetFields(AllFields))
            {
                var handle = MetadataTokens.FieldDefinitionHandle(++offset.FieldIndex);
                _metadata.ReserveFieldDefinition(field, handle);
            }

            foreach (var property in type.GetProperties(AllProperties))
            {
                // We don't need to handle backing field. Because, it's handled as a regular field.
                // Also, we don't need to handle getter or setter. Because, they are handled as regular methods.
                var propertyHandle = MetadataTokens.PropertyDefinitionHandle(++offset.PropertyIndex);
                _metadata.ReservePropertyDefinition(property, propertyHandle);
            }

            foreach (var ev in type.GetEvents(AllEvents))
            {
                var eventHandle = MetadataTokens.EventDefinitionHandle(++offset.EventIndex);
                _metadata.ReserveEventDefinition(ev, eventHandle);
            }

            foreach (var ctor in type.GetConstructors(AllMethods))
            {
                var handle = MetadataTokens.MethodDefinitionHandle(++offset.MethodIndex);
                _metadata.ReserveConstructorDefinition(ctor, handle);
            }

            foreach (var method in type.GetMethods(AllMethods))
            {
                var handle = MetadataTokens.MethodDefinitionHandle(++offset.MethodIndex);
                _metadata.ReserveMethodDefinition(method, handle);
            }
        }
        private void CreateTypes(IEnumerable <Type> types)
        {
            var offsets = new TypeDefinitionMetadataOffset()
            {
                TypeIndex     = _metadata.Builder.GetRowCount(TableIndex.TypeDef),
                FieldIndex    = _metadata.Builder.GetRowCount(TableIndex.Field),
                PropertyIndex = _metadata.Builder.GetRowCount(TableIndex.PropertyMap),
                MethodIndex   = _metadata.Builder.GetRowCount(TableIndex.MethodDef),
                EventIndex    = _metadata.Builder.GetRowCount(TableIndex.EventMap)
            };

            // Reserve types
            foreach (var type in types)
            {
                ReserveType(type, ref offsets);
            }

            // Create types
            foreach (var type in types)
            {
                CreateType(type);
            }
        }
Exemple #4
0
        private TypeDefinitionMetadataOffset ReserveTypeDefinition(Type type, TypeDefinitionMetadataOffset offset)
        {
            var baseTypeHandle = type.BaseType != null?_metadata.GetTypeHandle(type.BaseType) : default;

            var fieldRowCount    = offset.FieldIndex;
            var propertyRowCount = offset.PropertyIndex;
            var methodRowCount   = offset.MethodIndex;
            var eventRowCount    = offset.EventIndex;

            foreach (var field in type.GetFields(AllFields))
            {
                var handle = MetadataTokens.FieldDefinitionHandle(fieldRowCount + 1);
                _metadata.ReserveFieldDefinition(field, handle);
                ++fieldRowCount;
            }

            foreach (var property in type.GetProperties(AllProperties))
            {
                // We don't need to handle backing field. Because, it's handled as a regular field.
                // Also, we don't need to handle getter or setter. Because, they are handled as regular methods.
                var propertyHandle = MetadataTokens.PropertyDefinitionHandle(propertyRowCount + 1);
                _metadata.ReservePropertyDefinition(property, propertyHandle);
                ++propertyRowCount;
            }

            foreach (var ev in type.GetEvents(AllEvents))
            {
                var eventHandle = MetadataTokens.EventDefinitionHandle(eventRowCount + 1);
                _metadata.ReserveEventDefinition(ev, eventHandle);
                ++eventRowCount;
            }

            foreach (var ctor in type.GetConstructors(AllMethods))
            {
                var handle = MetadataTokens.MethodDefinitionHandle(methodRowCount + 1);
                _metadata.ReserveConstructorDefinition(ctor, handle);
                ++methodRowCount;
            }

            foreach (var method in type.GetMethods(AllMethods))
            {
                var handle = MetadataTokens.MethodDefinitionHandle(methodRowCount + 1);
                _metadata.ReserveMethodDefinition(method, handle);
                ++methodRowCount;
            }

            var typeHandle = _metadata.Builder.AddTypeDefinition(
                type.Attributes,
                _metadata.GetOrAddString(ApplyNameChange(type.Namespace)),
                _metadata.GetOrAddString(type.Name),
                baseTypeHandle,
                MetadataTokens.FieldDefinitionHandle(offset.FieldIndex + 1),
                MetadataTokens.MethodDefinitionHandle(offset.MethodIndex + 1));

            // Add immediately to support self referencing generics
            _metadata.ReserveTypeDefinition(type, typeHandle, offset.FieldIndex, offset.PropertyIndex,
                                            offset.MethodIndex, offset.EventIndex);

            // Handle generics type
            if (type.IsGenericType)
            {
                if (type.IsGenericTypeDefinition)
                {
                    var genericType = type.GetGenericTypeDefinition();
                    var typeInfo    = genericType.GetTypeInfo();

                    for (var i = 0; i < typeInfo.GenericTypeParameters.Length; ++i)
                    {
                        var parm = typeInfo.GenericTypeParameters[i];
                        var attr = parm.GenericParameterAttributes;

                        var genericParameterHandle =
                            _metadata.Builder.AddGenericParameter(typeHandle, attr, _metadata.GetOrAddString(parm.Name),
                                                                  i);

                        foreach (var constraint in parm.GetGenericParameterConstraints())
                        {
                            _metadata.Builder.AddGenericParameterConstraint(genericParameterHandle,
                                                                            _metadata.GetTypeHandle(constraint));
                        }
                    }
                }
            }

            return(new TypeDefinitionMetadataOffset
            {
                FieldIndex = fieldRowCount,
                PropertyIndex = propertyRowCount,
                MethodIndex = methodRowCount,
                EventIndex = eventRowCount
            });
        }