Exemple #1
0
        /// <summary>
        /// Clones this method
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static MethodDefinition Clone(this MethodDefinition method, MemberImportingSession session)
        {
            var m = new MethodDefinition(method.Name, method.Attributes, method.ReturnType)
            {
                CallingConvention = method.CallingConvention,
                ExplicitThis      = method.ExplicitThis,
                HasThis           = method.HasThis,
                ImplAttributes    = method.ImplAttributes,
                MetadataToken     = new MetadataToken(method.MetadataToken.TokenType, GlobalContainer.GetFreeRID(session.DestinationModule)),
                MethodReturnType  = method.MethodReturnType,
                PInvokeInfo       = method.PInvokeInfo
            };

            m.IsPInvokeImpl = m.PInvokeInfo != null;
            var body = m.Body;

            body.GetType().GetField("code_size", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(body, method.Body.CodeSize);
            foreach (var x in method.CustomAttributes)
            {
                m.CustomAttributes.Add(x);
            }
            foreach (var x in method.GenericParameters)
            {
                m.GenericParameters.Add(x);
            }
            foreach (var x in method.Overrides)
            {
                m.Overrides.Add(x);
            }
            foreach (var x in method.SecurityDeclarations)
            {
                m.SecurityDeclarations.Add(x);
            }
            return(m);
        }
Exemple #2
0
        /// <summary>
        /// Clones this type
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public static TypeDefinition Clone(this TypeDefinition type, MemberImportingSession session)
        {
            var t = new TypeDefinition(type.Namespace, type.Name, type.Attributes, type.BaseType)
            {
                PackingSize   = type.PackingSize,
                ClassSize     = type.ClassSize,
                HasSecurity   = type.HasSecurity,
                MetadataToken = new MetadataToken(type.MetadataToken.TokenType, GlobalContainer.GetFreeRID(session.DestinationModule))
            };

            foreach (var x in type.Interfaces)
            {
                t.Interfaces.Add(x);
            }
            foreach (var x in type.SecurityDeclarations)
            {
                t.SecurityDeclarations.Add(x);
            }
            foreach (var x in type.CustomAttributes)
            {
                t.CustomAttributes.Add(x);
            }
            foreach (var x in type.GenericParameters)
            {
                t.GenericParameters.Add(x);
            }
            return(t);
        }
Exemple #3
0
        /// <summary>
        /// Creates a new MemberImporter to import a type to destination, automatically adding any other required importer
        /// </summary>
        /// <param name="type"></param>
        /// <param name="destType"></param>
        /// <param name="importList"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static MemberImporter CreateTypeImporter(TypeReference type, MemberImportingSession session, List <MemberImporter> importList, MemberImportingOptions options)
        {
            //Return value
            MemberImporter ret;

            //Checks if the type is a generic instance type or a generic parameter
            if (type is GenericParameter)
            {
                ret = MemberImporter.Create((_, __) => type);
            }
            else if (type is TypeSpecification)
            {
                ret = new TypeSpecificationImporter((TypeSpecification)type, session).Scan(options);
            }
            else
            {
                //Checks if the importer is in the dictionary
                MemberImporter importer;
                if (session.RegisteredImporters.TryGetValue(type, out importer))
                {
                    return(importer);
                }

                //Creates the importer, registers it and returns
                importer = Helpers.CreateTypeImporterForTypeDefinition(((TypeReference)type).Resolve(), session, importList, options);
                session.RegisteredImporters.Add(type, importer);
                importer.Scan(options);
                importList.Add(importer);
                return(importer);
            }

            //Adds the return value to the import list and returns
            importList.Add(ret);
            return(ret);
        }
Exemple #4
0
        /// <summary>
        /// Clones this property
        /// </summary>
        /// <param name="prop"></param>
        /// <returns></returns>
        public static PropertyDefinition Clone(this PropertyDefinition prop, MemberImportingSession session)
        {
            var p = new PropertyDefinition(prop.Name, prop.Attributes, prop.PropertyType)
            {
                GetMethod     = prop.GetMethod,
                SetMethod     = prop.SetMethod,
                MetadataToken = new MetadataToken(prop.MetadataToken.TokenType, GlobalContainer.GetFreeRID(session.DestinationModule))
            };

            foreach (var x in prop.CustomAttributes)
            {
                p.CustomAttributes.Add(x);
            }
            return(p);
        }
Exemple #5
0
        /// <summary>
        /// Clones this event
        /// </summary>
        /// <param name="prop"></param>
        /// <returns></returns>
        public static EventDefinition Clone(this EventDefinition evt, MemberImportingSession session)
        {
            var e = new EventDefinition(evt.Name, evt.Attributes, evt.EventType)
            {
                AddMethod     = evt.AddMethod,
                InvokeMethod  = evt.InvokeMethod,
                MetadataToken = new MetadataToken(evt.MetadataToken.TokenType, GlobalContainer.GetFreeRID(session.DestinationModule)),
                RemoveMethod  = evt.RemoveMethod
            };

            foreach (var x in evt.CustomAttributes)
            {
                e.CustomAttributes.Add(x);
            }
            return(e);
        }
Exemple #6
0
        /// <summary>
        /// Clones this field
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public static FieldDefinition Clone(this FieldDefinition field, MemberImportingSession session)
        {
            var f = new FieldDefinition(field.Name, field.Attributes, field.FieldType)
            {
                Offset        = field.Offset,
                InitialValue  = field.InitialValue,
                HasConstant   = field.HasConstant,
                Constant      = field.Constant,
                MarshalInfo   = field.MarshalInfo,
                HasDefault    = field.HasDefault,
                MetadataToken = new MetadataToken(field.MetadataToken.TokenType, GlobalContainer.GetFreeRID(session.DestinationModule))
            };

            foreach (var x in field.CustomAttributes)
            {
                f.CustomAttributes.Add(x);
            }
            return(f);
        }
 public AssemblyReferenceImporter(IMetadataTokenProvider member, MemberImportingSession session)
     : base(member, session.DestinationModule, session)
 {
 }
Exemple #8
0
 public TypeSpecificationImporter(TypeSpecification type, MemberImportingSession session)
     : base(type, session)
 {
 }
 public GenericParametersImporter(IMetadataTokenProvider member, IMetadataTokenProvider destination, MemberImportingSession session)
     : base(member, destination, session)
 {
 }
Exemple #10
0
 public AssemblyReferenceImporter(IMetadataTokenProvider member, MemberImportingSession session)
     : base(member, session.DestinationModule, session)
 {
 }
 public GenericInstanceTypeImporter(IMetadataTokenProvider member, IMetadataTokenProvider destination, MemberImportingSession session)
     : base(member, destination, session)
 {
 }
Exemple #12
0
 public FieldImporter(IMetadataTokenProvider member, IMetadataTokenProvider destination, MemberImportingSession session)
     : this(member, destination, session, true)
 {
 }
Exemple #13
0
        /// <summary>
        /// Returns the constructor matching the provided arguments
        /// </summary>
        /// <param name="type">Type in which the constructor resides</param>
        /// <param name="pars">Parameters of the constructor</param>
        /// <returns></returns>
        public static MethodReference GetConstructorMatchingArguments(TypeDefinition type, IEnumerable <CustomAttributeArgument> pars, MemberImportingSession session)
        {
            var ctor =
                type.Methods
                .Where(x => x.Name == ".ctor")
                .Where(x => x.Parameters.Count == pars.Count())
                .FirstOrDefault(m => m.Parameters.Select((x, i) => Tuple.Create(x, i)).All(p => p.Item1.ParameterType.Name == pars.ElementAt(p.Item2).Type.Name&& p.Item1.ParameterType.Namespace == pars.ElementAt(p.Item2).Type.Namespace));

            return(ctor == null ? null : session.DestinationModule.Import(ctor));
        }
Exemple #14
0
        private static MemberImporter CreateTypeImporterForTypeDefinition(TypeDefinition type, MemberImportingSession session, List <MemberImporter> importList, MemberImportingOptions options)
        {
            //Checks if the type is accessible
            if (
                (session.DestinationType != null && Helpers.IsTypeAccessibleFrom(type, session.DestinationType)) ||
                Helpers.IsTypeAccessibleFrom(type, session.DestinationModule)
                )
            {
                //Queues addition of an assembly reference
                if (type.Module != session.DestinationModule)
                {
                    if (!session.DestinationModule.AssemblyReferences.Any(x => x.FullName == type.Module.Assembly.Name.FullName))
                    {
                        //Checks if the reference importer is already in the dictionary
                        MemberImporter importer;
                        if (!session.RegisteredImporters.TryGetValue(type.Module.Assembly.Name, out importer))
                        {
                            importList.Add(importer = new AssemblyReferenceImporter(type.Module.Assembly.Name, session).Scan(options));
                            session.RegisteredImporters.Add(type.Module.Assembly.Name, importer);
                        }
                    }
                }

                //Creates the type importer
                return(new TypeReferenceInModuleImporter(type, session));
            }
            else
            {
                //Creates the type importer
                return(new TypeImporter(type, options.ImportAsNestedType ? (IMetadataTokenProvider)session.DestinationType : (IMetadataTokenProvider)session.DestinationModule, session));
            }
        }
Exemple #15
0
        /// <summary>
        /// Clones this generic parameter
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public static GenericParameter Clone(this GenericParameter param, IGenericParameterProvider owner, MemberImportingSession session)
        {
            var p = new GenericParameter(param.Name, owner)
            {
                Attributes    = param.Attributes,
                DeclaringType = param.DeclaringType,
                HasDefaultConstructorConstraint = param.HasDefaultConstructorConstraint,
                MetadataToken = new MetadataToken(param.MetadataToken.TokenType, GlobalContainer.GetFreeRID(session.DestinationModule))
            };

            foreach (var x in param.Constraints)
            {
                p.Constraints.Add(x);
            }
            foreach (var x in param.CustomAttributes)
            {
                p.CustomAttributes.Add(x);
            }
            foreach (var x in param.GenericParameters)
            {
                p.GenericParameters.Add(x);
            }
            return(p);
        }
Exemple #16
0
 public MethodImporter(IMetadataTokenProvider member, IMetadataTokenProvider destination, MemberImportingSession session)
     : this(member, destination, session, true)
 {
 }
Exemple #17
0
 public MethodImporter(IMetadataTokenProvider member, IMetadataTokenProvider destination, MemberImportingSession session, bool createNode)
     : base(member, destination, session)
 {
     _createNode = createNode;
 }
 public GenericParametersImporter(IMetadataTokenProvider member, IMetadataTokenProvider destination, MemberImportingSession session)
     : base(member, destination, session)
 {
 }
Exemple #19
0
 public FieldImporter(IMetadataTokenProvider member, IMetadataTokenProvider destination, MemberImportingSession session, bool createNode)
     : base(member, destination, session)
 {
     _createNode = createNode;
 }
 public TypeSpecificationImporter(TypeSpecification type, MemberImportingSession session)
     : base(type, session)
 {
 }
Exemple #21
0
 public TypeImporter(IMetadataTokenProvider member, IMetadataTokenProvider destination, MemberImportingSession session)
     : base(member, destination, session)
 {
 }
Exemple #22
0
 public CustomAttributesImporter(IMetadataTokenProvider member, IMetadataTokenProvider destination, MemberImportingSession session)
     : base(member, destination, session)
 {
 }