internal static EventDefinition Clone(EventDefinition evt, ImportContext context)
        {
            EventDefinition ne = new EventDefinition(
                evt.Name,
                context.Import(evt.EventType),
                evt.Attributes);

            if (context != null && context.GenericContext.Type is TypeDefinition)
            {
                TypeDefinition type = context.GenericContext.Type as TypeDefinition;
                if (evt.AddMethod != null)
                {
                    ne.AddMethod = type.Methods.GetMethod(evt.AddMethod.Name) [0];
                }
                if (evt.InvokeMethod != null)
                {
                    ne.InvokeMethod = type.Methods.GetMethod(evt.InvokeMethod.Name) [0];
                }
                if (evt.RemoveMethod != null)
                {
                    ne.RemoveMethod = type.Methods.GetMethod(evt.RemoveMethod.Name) [0];
                }
            }

            foreach (CustomAttribute ca in evt.CustomAttributes)
            {
                ne.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
            }

            return(ne);
        }
        internal static PropertyDefinition Clone(PropertyDefinition prop, ImportContext context)
        {
            PropertyDefinition np = new PropertyDefinition(
                prop.Name,
                context.Import(prop.PropertyType),
                prop.Attributes);

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

            if (context != null && context.GenericContext.Type is TypeDefinition)
            {
                TypeDefinition type = context.GenericContext.Type as TypeDefinition;
                if (prop.SetMethod != null)
                {
                    np.SetMethod = type.Methods.GetMethod(prop.SetMethod.Name) [0];
                }
                if (prop.GetMethod != null)
                {
                    np.GetMethod = type.Methods.GetMethod(prop.GetMethod.Name) [0];
                }
            }

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

            return(np);
        }
        internal static FieldDefinition Clone(FieldDefinition field, ImportContext context)
        {
            FieldDefinition nf = new FieldDefinition(
                field.Name,
                context.Import(field.FieldType),
                field.Attributes);

            if (field.HasConstant)
            {
                nf.Constant = field.Constant;
            }
            if (field.MarshalSpec != null)
            {
                nf.MarshalSpec = field.MarshalSpec;
            }
            if (field.RVA != RVA.Zero)
            {
                nf.InitialValue = field.InitialValue;
            }
            else
            {
                nf.InitialValue = new byte [0];
            }
            if (field.HasLayoutInfo)
            {
                nf.Offset = field.Offset;
            }

            foreach (CustomAttribute ca in field.CustomAttributes)
            {
                nf.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
            }

            return(nf);
        }
        internal static GenericParameter Clone(GenericParameter gp, ImportContext context)
        {
            GenericParameter ngp;

            if (gp.Owner is TypeReference)
            {
                ngp = new GenericParameter(gp.m_name, context.GenericContext.Type);
            }
            else if (gp.Owner is MethodReference)
            {
                ngp = new GenericParameter(gp.m_name, context.GenericContext.Method);
            }
            else
            {
                throw new NotSupportedException();
            }

            ngp.Position   = gp.Owner.GenericParameters.IndexOf(gp);
            ngp.Attributes = gp.Attributes;

            if (gp.HasCustomAttributes)
            {
                foreach (CustomAttribute ca in gp.CustomAttributes)
                {
                    ngp.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
                }
            }

            return(ngp);
        }
        internal static ParameterDefinition Clone(ParameterDefinition param, ImportContext context)
        {
            ParameterDefinition np = new ParameterDefinition(
                param.Name,
                param.Sequence,
                param.Attributes,
                context.Import(param.ParameterType));

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

            if (param.MarshalSpec != null)
            {
                np.MarshalSpec = param.MarshalSpec;
            }

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

            return(np);
        }
Esempio n. 6
0
        internal static MethodDefinition Clone(MethodDefinition meth, ImportContext context)
        {
            MethodDefinition nm = new MethodDefinition(meth.Name, RVA.Zero, meth.Attributes, meth.ImplAttributes, meth.HasThis, meth.ExplicitThis, meth.CallingConvention);

            context.GenericContext.Method = nm;

            foreach (GenericParameter p in meth.GenericParameters)
            {
                nm.GenericParameters.Add(GenericParameter.Clone(p, context));
            }

            nm.ReturnType.ReturnType = context.Import(meth.ReturnType.ReturnType);

            if (meth.ReturnType.HasConstant)
            {
                nm.ReturnType.Constant = meth.ReturnType.Constant;
            }

            if (meth.ReturnType.MarshalSpec != null)
            {
                nm.ReturnType.MarshalSpec = meth.ReturnType.MarshalSpec;
            }

            foreach (CustomAttribute ca in meth.ReturnType.CustomAttributes)
            {
                nm.ReturnType.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
            }
            if (meth.PInvokeInfo != null)
            {
                nm.PInvokeInfo = meth.PInvokeInfo;                 // TODO: import module ?
            }
            foreach (ParameterDefinition param in meth.Parameters)
            {
                nm.Parameters.Add(ParameterDefinition.Clone(param, context));
            }
            foreach (MethodReference ov in meth.Overrides)
            {
                nm.Overrides.Add(context.Import(ov));
            }
            foreach (CustomAttribute ca in meth.CustomAttributes)
            {
                nm.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
            }
            foreach (SecurityDeclaration sec in meth.SecurityDeclarations)
            {
                nm.SecurityDeclarations.Add(SecurityDeclaration.Clone(sec));
            }

            if (meth.Body != null)
            {
                nm.Body = MethodBody.Clone(meth.Body, nm, context);
            }

            return(nm);
        }
        internal static TypeDefinition Clone(TypeDefinition type, ImportContext context)
        {
            TypeDefinition nt = new TypeDefinition(
                type.Name,
                type.Namespace,
                type.Attributes);

            context.GenericContext.Type = nt;

            foreach (GenericParameter p in type.GenericParameters)
            {
                nt.GenericParameters.Add(GenericParameter.Clone(p, context));
            }

            if (type.BaseType != null)
            {
                nt.BaseType = context.Import(type.BaseType);
            }

            if (type.HasLayoutInfo)
            {
                nt.ClassSize   = type.ClassSize;
                nt.PackingSize = type.PackingSize;
            }

            foreach (FieldDefinition field in type.Fields)
            {
                nt.Fields.Add(FieldDefinition.Clone(field, context));
            }
            foreach (MethodDefinition ctor in type.Constructors)
            {
                nt.Constructors.Add(MethodDefinition.Clone(ctor, context));
            }
            foreach (MethodDefinition meth in type.Methods)
            {
                nt.Methods.Add(MethodDefinition.Clone(meth, context));
            }
            foreach (EventDefinition evt in type.Events)
            {
                nt.Events.Add(EventDefinition.Clone(evt, context));
            }
            foreach (PropertyDefinition prop in type.Properties)
            {
                nt.Properties.Add(PropertyDefinition.Clone(prop, context));
            }
            foreach (TypeReference intf in type.Interfaces)
            {
                nt.Interfaces.Add(context.Import(intf));
            }
            foreach (TypeDefinition nested in type.NestedTypes)
            {
                nt.NestedTypes.Add(Clone(nested, context));
            }
            foreach (CustomAttribute ca in type.CustomAttributes)
            {
                nt.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
            }
            foreach (SecurityDeclaration dec in type.SecurityDeclarations)
            {
                nt.SecurityDeclarations.Add(SecurityDeclaration.Clone(dec));
            }

            return(nt);
        }
Esempio n. 8
0
        void MarkCustomAttribute(CustomAttribute ca)
        {
            MarkMethod (ca.Constructor);

            if (!ca.Resolved) {
                ca = ca.Clone ();
                ca.Resolve ();
            }

            if (!ca.Resolved)
                return;

            MarkCustomAttributeParameters (ca);

            TypeReference constructor_type = ca.Constructor.DeclaringType;
            TypeDefinition type = constructor_type.Resolve ();
            if (type == null)
                throw new ResolutionException (constructor_type);

            MarkCustomAttributeProperties (ca, type);
            MarkCustomAttributeFields (ca, type);
        }