Ejemplo n.º 1
0
        private BlobHandle GetPropertySignature(PropertyInfo propertyInfo)
        {
            var parameters      = propertyInfo.GetIndexParameters();
            var countParameters = parameters.Length;
            var retType         = propertyInfo.PropertyType;

            // Work out if this is an instance property
            var eitherAccessor = propertyInfo.GetMethod ?? propertyInfo.SetMethod;

            System.Diagnostics.Debug.Assert(eitherAccessor != null);
            var isInstanceProperty = eitherAccessor.CallingConvention.HasFlag(CallingConventions.HasThis);

            var blob = MetadataHelper.BuildSignature(x => x.PropertySignature(isInstanceProperty)
                                                     .Parameters(
                                                         countParameters,
                                                         r => r.FromSystemType(retType, _metadata),
                                                         p =>
            {
                foreach (var par in parameters)
                {
                    var parEncoder = p.AddParameter();
                    parEncoder.Type().FromSystemType(par.ParameterType, _metadata);
                }
            }));

            return(_metadata.GetOrAddBlob(blob));
        }
        private BlobHandle GetPropertySignature(PropertyInfo propertyInfo)
        {
            var parameters      = propertyInfo.GetIndexParameters();
            var countParameters = parameters.Length;
            var retType         = propertyInfo.PropertyType;

            var blob = MetadataHelper.BuildSignature(x => x.PropertySignature()
                                                     .Parameters(
                                                         countParameters,
                                                         r => r.FromSystemType(retType, _metadata),
                                                         p =>
            {
                foreach (var par in parameters)
                {
                    var parEncoder = p.AddParameter();
                    parEncoder.Type().FromSystemType(par.ParameterType, _metadata);
                }
            }));

            return(_metadata.GetOrAddBlob(blob));
        }
Ejemplo n.º 3
0
        private void CreateMethod(MethodInfo method, List <DelayedWrite> genericParams)
        {
            if (!_metadata.TryGetMethodDefinition(method, out var metadata))
            {
                ThrowMetadataIsNotReserved("Method", method);
            }

            EnsureMetadataWasNotEmitted(metadata, method);

            var body = method.GetMethodBody();

            var localVariablesSignature = default(StandaloneSignatureHandle);

            if (body != null && body.LocalVariables.Count > 0)
            {
                localVariablesSignature = _metadata.Builder.AddStandaloneSignature(_metadata.GetOrAddBlob(
                                                                                       MetadataHelper.BuildSignature(x =>
                {
                    x.LocalVariableSignature(body.LocalVariables.Count).AddRange(body.LocalVariables, _metadata);
                })));
            }

            var offset = -1;

            // If body exists, we write it in IL body stream
            if (body != null && !method.IsAbstract)
            {
                var methodBodyWriter = new MethodBodyStreamWriter(_metadata);

                // offset can be aligned during serialization. So, override the correct offset.
                offset = methodBodyWriter.AddMethodBody(method, localVariablesSignature);
            }

            var parameters = CreateParameters(method.GetParameters());

            var handle = _metadata.Builder.AddMethodDefinition(
                method.Attributes,
                method.MethodImplementationFlags,
                _metadata.GetOrAddString(method.Name),
                _metadata.GetMethodOrConstructorSignature(method),
                offset,
                parameters);

            // The generation of interface method overrides has been moved to
            // AssemblyGenerator.DeclareInterfacesAndCreateInterfaceMap in
            // AssemblyGenerator.Types.cs.

            // Add generic parameters
            if (method.IsGenericMethodDefinition)
            {
                int index = 0;
                foreach (var arg in method.GetGenericArguments())
                {
                    genericParams.Add(new DelayedWrite(CodedIndex.TypeOrMethodDef(handle), () =>
                    {
                        // Add the argument
                        var gaHandle = _metadata.Builder.AddGenericParameter(handle, arg.GenericParameterAttributes, _metadata.GetOrAddString(arg.Name), index++);

                        // Add it's constraints
                        foreach (var constraint in arg.GetGenericParameterConstraints())
                        {
                            _metadata.Builder.AddGenericParameterConstraint(gaHandle, _metadata.GetTypeHandle(constraint));
                        }
                    }));
                }
            }
            else if (method.Attributes.HasFlag(MethodAttributes.PinvokeImpl))
            {
                ProcessPInvokeMapData(
                    method,
                    out string libraryName,
                    out string entryName,
                    out MethodImportAttributes attrs);

                var libraryNameHandle = _metadata.GetOrAddString(libraryName);
                var moduleRefHandle   = _metadata.Builder.AddModuleReference(libraryNameHandle);
                var entryNameHandle   = _metadata.GetOrAddString(entryName);

                // Add the ImplMap entry for the P/Invoke
                _metadata.Builder.AddMethodImport(
                    handle,
                    attrs,
                    entryNameHandle,
                    moduleRefHandle);
            }

            VerifyEmittedHandle(metadata, handle);
            metadata.MarkAsEmitted();

            CreateCustomAttributes(handle, method.GetCustomAttributesData());
        }
Ejemplo n.º 4
0
        private void CreateMethod(MethodInfo method)
        {
            if (!_metadata.TryGetMethodDefinition(method, out var metadata))
            {
                ThrowMetadataIsNotReserved("Method", method);
            }

            EnsureMetadataWasNotEmitted(metadata, method);

            var body = method.GetMethodBody();

            var localVariablesSignature = default(StandaloneSignatureHandle);

            if (body != null && body.LocalVariables.Count > 0)
            {
                localVariablesSignature = _metadata.Builder.AddStandaloneSignature(_metadata.GetOrAddBlob(
                                                                                       MetadataHelper.BuildSignature(x =>
                {
                    var sig = x.LocalVariableSignature(body.LocalVariables.Count);
                    foreach (var vrb in body.LocalVariables)
                    {
                        sig.AddVariable().Type(
                            vrb.LocalType.IsByRef,
                            vrb.IsPinned)
                        .FromSystemType(vrb.LocalType, _metadata);
                    }
                })));
            }

            var offset = _metadata.ILBuilder.Count; // take an offset

            // If body exists, we write it in IL body stream
            if (body != null)
            {
                var methodBodyWriter = new MethodBodyStreamWriter(_metadata);

                // offset can be aligned during serialization. So, override the correct offset.
                offset = methodBodyWriter.AddMethodBody(method, localVariablesSignature);
            }

            var parameters = CreateParameters(method.GetParameters());

            var handle = _metadata.Builder.AddMethodDefinition(
                method.Attributes,
                method.MethodImplementationFlags,
                _metadata.GetOrAddString(method.Name),
                _metadata.GetMethodSignature(method),
                offset,
                parameters);

            VerifyEmittedHandle(metadata, handle);
            metadata.MarkAsEmitted();

            CreateCustomAttributes(handle, method.GetCustomAttributesData());
        }
        private void CreateConstructor(ConstructorInfo ctor)
        {
            if (!_metadata.TryGetConstructorDefinition(ctor, out var metadata))
            {
                ThrowMetadataIsNotReserved("Constructor", ctor);
            }

            EnsureMetadataWasNotEmitted(metadata, ctor);

            var body = ctor.GetMethodBody();

            var localVariablesSignature = default(StandaloneSignatureHandle);

            if (body != null && body.LocalVariables.Count > 0)
            {
                localVariablesSignature = _metadata.Builder.AddStandaloneSignature(_metadata.GetOrAddBlob(
                                                                                       MetadataHelper.BuildSignature(x =>
                {
                    x.LocalVariableSignature(body.LocalVariables.Count).AddRange(body.LocalVariables, _metadata);
                })));
            }

            var bodyOffset = _metadata.ILBuilder.Count;

            if (body != null)
            {
                var methodBodyWriter = new MethodBodyStreamWriter(_metadata);

                // bodyOffset can be aligned during serialization. So, override the correct offset.
                bodyOffset = methodBodyWriter.AddMethodBody(ctor, localVariablesSignature);
            }

            var parameters = CreateParameters(ctor.GetParameters());

            var handle = _metadata.Builder.AddMethodDefinition(
                ctor.Attributes,
                ctor.MethodImplementationFlags,
                _metadata.GetOrAddString(ctor.Name),
                _metadata.GetMethodOrConstructorSignature(ctor),
                bodyOffset,
                parameters);

            VerifyEmittedHandle(metadata, handle);
            metadata.MarkAsEmitted();
        }
        private void CreateMethod(MethodInfo method, List <DelayedWrite> genericParams)
        {
            if (!_metadata.TryGetMethodDefinition(method, out var metadata))
            {
                ThrowMetadataIsNotReserved("Method", method);
            }

            EnsureMetadataWasNotEmitted(metadata, method);

            var body = method.GetMethodBody();

            var localVariablesSignature = default(StandaloneSignatureHandle);

            if (body != null && body.LocalVariables.Count > 0)
            {
                localVariablesSignature = _metadata.Builder.AddStandaloneSignature(_metadata.GetOrAddBlob(
                                                                                       MetadataHelper.BuildSignature(x =>
                {
                    x.LocalVariableSignature(body.LocalVariables.Count).AddRange(body.LocalVariables, _metadata);
                })));
            }

            var offset = -1;

            // If body exists, we write it in IL body stream
            if (body != null && !method.IsAbstract)
            {
                var methodBodyWriter = new MethodBodyStreamWriter(_metadata);

                // offset can be aligned during serialization. So, override the correct offset.
                offset = methodBodyWriter.AddMethodBody(method, localVariablesSignature);
            }

            var parameters = CreateParameters(method.GetParameters());

            var handle = _metadata.Builder.AddMethodDefinition(
                method.Attributes,
                method.MethodImplementationFlags,
                _metadata.GetOrAddString(method.Name),
                _metadata.GetMethodOrConstructorSignature(method),
                offset,
                parameters);

            // Explicit interface implementations need to be marked with method implementation
            // (This is the equivalent of .Override in msil)
            if (method.IsPrivate)
            {
                // Go through all the implemented interfaces and all their methods
                // looking for methods that this method implements and mark accordingly.

                // NB: This is not super efficient.  Should probably create a map somewhere
                //     for faster lookup, but this will do for now.

                var type = method.DeclaringType;
                foreach (var itf in type.GetInterfaces())
                {
                    var itfMap = type.GetInterfaceMap(itf);
                    for (int i = 0; i < itfMap.TargetMethods.Length; i++)
                    {
                        var m = itfMap.TargetMethods[i];
                        if (m == method)
                        {
                            var itfImpl = itfMap.InterfaceMethods[i];
                            _metadata.Builder.AddMethodImplementation((TypeDefinitionHandle)_metadata.GetTypeHandle(method.DeclaringType), handle, _metadata.GetMethodHandle(itfImpl));
                        }
                    }
                }
            }

            // Add generic parameters
            if (method.IsGenericMethodDefinition)
            {
                int index = 0;
                foreach (var arg in method.GetGenericArguments())
                {
                    genericParams.Add(new DelayedWrite(CodedIndex.TypeOrMethodDef(handle), () =>
                    {
                        // Add the argument
                        var gaHandle = _metadata.Builder.AddGenericParameter(handle, arg.GenericParameterAttributes, _metadata.GetOrAddString(arg.Name), index++);

                        // Add it's constraints
                        foreach (var constraint in arg.GetGenericParameterConstraints())
                        {
                            _metadata.Builder.AddGenericParameterConstraint(gaHandle, _metadata.GetTypeHandle(constraint));
                        }
                    }));
                }
            }

            VerifyEmittedHandle(metadata, handle);
            metadata.MarkAsEmitted();

            CreateCustomAttributes(handle, method.GetCustomAttributesData());
        }