Пример #1
0
        public static ParameterDefinition CloneParameterDefinition(ParameterDefinition param, MethodDefinition owner)
        {
            var context = owner.Module;
            var np      = new ParameterDefinition(
                param.Name,
                param.Attributes,
                CecilImporter.Import(context, param.ParameterType));

            if (param.HasConstant)
            {
                np.Constant = param.Constant;
            }

            if (param.MarshalInfo != null)
            {
                np.MarshalInfo = new MarshalInfo(param.MarshalInfo.NativeType);
            }

            foreach (var ca in param.CustomAttributes)
            {
                np.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
            }

            return(np);
        }
Пример #2
0
        /// <summary>
        /// Retrieve a 'delegate' method from a module definition
        /// </summary>
        /// <param name="modef">Module definition</param>
        /// <param name="name">'delegate' method name</param>
        /// <returns>method reference</returns>
        private static MethodReference GetDelegateMethod(ModuleDefinition modef, string name)
        {
            var tref = modef.TypeSystem.Delegate;
            var tdef = tref.Resolve();

            if (tdef == null)
            {
                return(null);
            }

            return((tdef.Methods.Where(mdef => mdef.Name.Equals(name) && mdef.Parameters.Count == 2)
                    .Select(m => CecilImporter.Import(modef, m))).FirstOrDefault());
        }
Пример #3
0
        private static MethodReference GetDefaultConstructor(ModuleDefinition modef, TypeReference tref)
        {
            var tdef = tref.Resolve();

            if (tdef == null)
            {
                return(null);
            }

            var ctor = tdef.Methods.FirstOrDefault(mdef => mdef.IsConstructor && mdef.Parameters.Count == 0);

            return(ctor == null ? null : CecilImporter.Import(modef, ctor));
        }
Пример #4
0
        private static TypeReference FixTypeImport(ModuleDefinition context, MethodDefinition source, MethodDefinition target,
                                                   TypeReference type)
        {
            if (type is TypeDefinition)
            {
                var result = FindMatchingType(context, type.FullName);
                if (result == null)
                {
                    throw new ArgumentException(string.Format("No match for type {0} in source assembly.", type.FullName));
                }
                return(result);
            }

            return(CecilImporter.Import(context, type));
        }
Пример #5
0
        private static MethodReference FixMethodImport(ModuleDefinition context, MethodDefinition source,
                                                       MethodDefinition target, MethodReference method)
        {
            if (method is MethodDefinition)
            {
                var type   = FixTypeImport(context, source, target, method.DeclaringType) as TypeDefinition;
                var result = FindMatchingMethod(type, method);
                if (result == null)
                {
                    throw new ArgumentException(string.Format("No match for method {0} in source assembly.", method.FullName));
                }
                return(result);
            }

            return(CecilImporter.Import(context, method));
        }
Пример #6
0
        private static FieldReference FixFieldImport(ModuleDefinition context, FieldReference field)
        {
            if (!(field is FieldDefinition))
            {
                return(CecilImporter.Import(context, field));
            }

            var type   = FixTypeImport(context, field.DeclaringType) as TypeDefinition;
            var result = FindMatchingField(type, field);

            if (result == null)
            {
                throw new ArgumentException(string.Format("No match for field {0} in source assembly.", field.FullName));
            }

            return(result);
        }
Пример #7
0
        /// <summary>
        /// Inject an item definition into an owner
        /// </summary>
        /// <param name="owner">Owner item</param>
        /// <param name="targettype">Target type definition</param>
        /// <param name="name">name for the newly created item</param>
        /// <param name="extratype">Extra type</param>
        /// <returns>Object definition</returns>
        public static object Inject(object owner, InjectType targettype, string name, object extratype)
        {
            if (owner == null || name == null)
            {
                throw new ArgumentException();
            }

            if (owner is AssemblyDefinition)
            {
                var adef = owner as AssemblyDefinition;
                switch (targettype)
                {
                case InjectType.AssemblyReference:
                    return(InjectAssemblyNameReference(adef, name));

                case InjectType.Type:
                    return(InjectTypeDefinition(adef.MainModule, name, CecilImporter.Import(adef.MainModule, extratype as TypeReference)));

                case InjectType.Class:
                    return(InjectClassDefinition(adef.MainModule, name, CecilImporter.Import(adef.MainModule, extratype as TypeReference)));

                case InjectType.Interface:
                    return(InjectInterfaceDefinition(adef.MainModule, name));

                case InjectType.Struct:
                    return(InjectStructDefinition(adef.MainModule, name));

                case InjectType.Enum:
                    return(InjectEnumDefinition(adef.MainModule, name));

                case InjectType.Resource:
                    return(InjectResource(adef.MainModule, name, (ResourceType)extratype));
                }
            }
            else if (owner is TypeDefinition)
            {
                var tdef = owner as TypeDefinition;

                switch (targettype)
                {
                case InjectType.Type:
                    return(InjectInnerTypeDefinition(tdef, name, CecilImporter.Import(tdef.Module, extratype as TypeReference)));

                case InjectType.Class:
                    return(InjectInnerClassDefinition(tdef, name, CecilImporter.Import(tdef.Module, extratype as TypeReference)));

                case InjectType.Interface:
                    return(InjectInnerInterfaceDefinition(tdef, name));

                case InjectType.Struct:
                    return(InjectInnerStructDefinition(tdef, name));

                case InjectType.Enum:
                    return(InjectInnerEnumDefinition(tdef, name));

                case InjectType.Constructor:
                    return(InjectConstructorDefinition(tdef));

                case InjectType.Method:
                    return(InjectMethodDefinition(tdef, name));

                case InjectType.Property:
                    return(InjectPropertyDefinition(tdef, name, CecilImporter.Import(tdef.Module, extratype as TypeReference)));

                case InjectType.Field:
                    return(InjectFieldDefinition(tdef, name, CecilImporter.Import(tdef.Module, extratype as TypeReference)));

                case InjectType.Event:
                    return(InjectEventDefinition(tdef, name, CecilImporter.Import(tdef.Module, extratype as TypeReference)));
                }
            }

            throw new NotImplementedException();
        }