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.CloneInto(nf); } 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 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); }
internal static CustomAttribute Clone(CustomAttribute custattr, ImportContext context) { CustomAttribute ca = new CustomAttribute(context.Import(custattr.Constructor)); custattr.CopyTo(ca); return(ca); }
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.GenericContext.Type is TypeDefinition) { TypeDefinition type = context.GenericContext.Type as TypeDefinition; if (prop.SetMethod != null) { np.SetMethod = type.Methods.GetMethod(prop.SetMethod.Name, prop.SetMethod.Parameters); } if (prop.GetMethod != null) { np.GetMethod = type.Methods.GetMethod(prop.GetMethod.Name, prop.GetMethod.Parameters); } } foreach (CustomAttribute ca in prop.CustomAttributes) { np.CustomAttributes.Add(CustomAttribute.Clone(ca, context)); } return(np); }
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; foreach (TypeReference constraint in gp.Constraints) { ngp.Constraints.Add(context.Import(constraint)); } foreach (CustomAttribute ca in gp.CustomAttributes) { ngp.CustomAttributes.Add(CustomAttribute.Clone(ca, context)); } return(ngp); }
static void CloneConstraints(GenericParameter gp, GenericParameter ngp, ImportContext context) { if (gp.HasConstraints) { foreach (TypeReference constraint in gp.Constraints) { ngp.Constraints.Add(context.Import(constraint)); } } }
internal static CustomAttribute Clone(CustomAttribute custattr, ImportContext context) { CustomAttribute ca = new CustomAttribute(context.Import(custattr.Constructor)); if (!custattr.IsReadable) { ca.IsReadable = false; ca.Blob = custattr.Blob; return(ca); } foreach (object o in custattr.ConstructorParameters) { ca.ConstructorParameters.Add(o); } Clone(custattr.Fields, ca.Fields); Clone(custattr.FieldTypes, ca.FieldTypes); Clone(custattr.Properties, ca.Properties); Clone(custattr.PropertyTypes, ca.PropertyTypes); return(ca); }
internal static MethodBody Clone (MethodBody body, MethodDefinition parent, ImportContext context) { MethodBody nb = new MethodBody (parent); nb.MaxStack = body.MaxStack; nb.InitLocals = body.InitLocals; nb.CodeSize = body.CodeSize; foreach (VariableDefinition var in body.Variables) nb.Variables.Add (new VariableDefinition ( context.Import (var.VariableType))); foreach (Instruction instr in body.Instructions) { Instruction ni = new Instruction (instr.OpCode); switch (instr.OpCode.OperandType) { case OperandType.InlineParam : case OperandType.ShortInlineParam : int param = body.Method.Parameters.IndexOf ((ParameterDefinition) instr.Operand); ni.Operand = parent.Parameters [param]; break; case OperandType.InlineVar : case OperandType.ShortInlineVar : int var = body.Variables.IndexOf ((VariableDefinition) instr.Operand); ni.Operand = nb.Variables [var]; break; case OperandType.InlineField : ni.Operand = context.Import ((FieldReference) instr.Operand); break; case OperandType.InlineMethod : ni.Operand = context.Import ((MethodReference) instr.Operand); break; case OperandType.InlineType : ni.Operand = context.Import ((TypeReference) instr.Operand); break; case OperandType.InlineTok : if (instr.Operand is TypeReference) ni.Operand = context.Import ((TypeReference) instr.Operand); else if (instr.Operand is FieldReference) ni.Operand = context.Import ((FieldReference) instr.Operand); else if (instr.Operand is MethodReference) ni.Operand = context.Import ((MethodReference) instr.Operand); break; case OperandType.ShortInlineBrTarget : case OperandType.InlineBrTarget : break; default : ni.Operand = instr.Operand; break; } nb.Instructions.Add (ni); } for (int i = 0; i < body.Instructions.Count; i++) { Instruction instr = nb.Instructions [i]; if (instr.OpCode.OperandType != OperandType.ShortInlineBrTarget && instr.OpCode.OperandType != OperandType.InlineBrTarget) continue; instr.Operand = GetInstruction (body, nb, (Instruction) body.Instructions [i].Operand); } foreach (ExceptionHandler eh in body.ExceptionHandlers) { ExceptionHandler neh = new ExceptionHandler (eh.Type); neh.TryStart = GetInstruction (body, nb, eh.TryStart); neh.TryEnd = GetInstruction (body, nb, eh.TryEnd); neh.HandlerStart = GetInstruction (body, nb, eh.HandlerStart); neh.HandlerEnd = GetInstruction (body, nb, eh.HandlerEnd); switch (eh.Type) { case ExceptionHandlerType.Catch : neh.CatchType = context.Import (eh.CatchType); break; case ExceptionHandlerType.Filter : neh.FilterStart = GetInstruction (body, nb, eh.FilterStart); neh.FilterEnd = GetInstruction (body, nb, eh.FilterEnd); break; } nb.ExceptionHandlers.Add (neh); } return nb; }
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 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 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.CloneInto (nf); 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 CustomAttribute Clone (CustomAttribute custattr, ImportContext context) { CustomAttribute ca = new CustomAttribute (context.Import (custattr.Constructor)); custattr.CopyTo (ca); return ca; }
internal static CustomAttribute Clone(CustomAttribute custattr, ImportContext context) { CustomAttribute ca = new CustomAttribute (context.Import (custattr.Constructor)); if (!custattr.IsReadable) { ca.IsReadable = false; ca.Blob = custattr.Blob; return ca; } foreach (object o in custattr.ConstructorParameters) ca.ConstructorParameters.Add (o); Clone (custattr.Fields, ca.Fields); Clone (custattr.FieldTypes, ca.FieldTypes); Clone (custattr.Properties, ca.Properties); Clone (custattr.PropertyTypes, ca.PropertyTypes); return ca; }
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; foreach (TypeReference constraint in gp.Constraints) ngp.Constraints.Add (context.Import (constraint)); foreach (CustomAttribute ca in gp.CustomAttributes) ngp.CustomAttributes.Add (CustomAttribute.Clone (ca, context)); return ngp; }
static void CloneConstraints (GenericParameter gp, GenericParameter ngp, ImportContext context) { if (gp.HasConstraints) { foreach (TypeReference constraint in gp.Constraints) ngp.Constraints.Add (context.Import (constraint)); } }
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; }
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); }
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); MethodReference contextMethod = context.GenericContext.Method; context.GenericContext.Method = nm; GenericParameter.CloneInto(meth, nm, context); nm.ReturnType.ReturnType = context.Import(meth.ReturnType.ReturnType); if (meth.ReturnType.Parameter != null) { nm.ReturnType.Parameter = ParameterDefinition.Clone(meth.ReturnType.Parameter, context); nm.ReturnType.Parameter.Method = nm; } if (meth.PInvokeInfo != null) { nm.PInvokeInfo = meth.PInvokeInfo; // TODO: import module ? } if (meth.HasParameters) { foreach (ParameterDefinition param in meth.Parameters) { nm.Parameters.Add(ParameterDefinition.Clone(param, context)); } } if (meth.HasOverrides) { foreach (MethodReference ov in meth.Overrides) { nm.Overrides.Add(context.Import(ov)); } } if (meth.HasCustomAttributes) { foreach (CustomAttribute ca in meth.CustomAttributes) { nm.CustomAttributes.Add(CustomAttribute.Clone(ca, context)); } } if (meth.HasSecurityDeclarations) { foreach (SecurityDeclaration sec in meth.SecurityDeclarations) { nm.SecurityDeclarations.Add(SecurityDeclaration.Clone(sec)); } } if (meth.Body != null) { nm.Body = MethodBody.Clone(meth.Body, nm, context); } context.GenericContext.Method = contextMethod; 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; }
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.GenericContext.Type is TypeDefinition) { TypeDefinition type = context.GenericContext.Type as TypeDefinition; if (prop.SetMethod != null) np.SetMethod = type.Methods.GetMethod (prop.SetMethod.Name, prop.SetMethod.Parameters); if (prop.GetMethod != null) np.GetMethod = type.Methods.GetMethod (prop.GetMethod.Name, prop.GetMethod.Parameters); } foreach (CustomAttribute ca in prop.CustomAttributes) np.CustomAttributes.Add (CustomAttribute.Clone (ca, context)); return np; }
internal static MethodBody Clone (MethodBody body, MethodDefinition parent, ImportContext context) { MethodBody nb = new MethodBody (parent); nb.MaxStack = body.MaxStack; nb.InitLocals = body.InitLocals; nb.CodeSize = body.CodeSize; CilWorker worker = nb.CilWorker; if (body.HasVariables) { foreach (VariableDefinition var in body.Variables) nb.Variables.Add (new VariableDefinition ( var.Name, var.Index, parent, context.Import (var.VariableType))); } foreach (Instruction instr in body.Instructions) { Instruction ni = new Instruction (instr.OpCode); switch (instr.OpCode.OperandType) { case OperandType.InlineParam : case OperandType.ShortInlineParam : if (instr.Operand == body.Method.This) ni.Operand = nb.Method.This; else { int param = body.Method.Parameters.IndexOf ((ParameterDefinition) instr.Operand); ni.Operand = parent.Parameters [param]; } break; case OperandType.InlineVar : case OperandType.ShortInlineVar : int var = body.Variables.IndexOf ((VariableDefinition) instr.Operand); ni.Operand = nb.Variables [var]; break; case OperandType.InlineField : ni.Operand = context.Import ((FieldReference) instr.Operand); break; case OperandType.InlineMethod : ni.Operand = context.Import ((MethodReference) instr.Operand); break; case OperandType.InlineType : ni.Operand = context.Import ((TypeReference) instr.Operand); break; case OperandType.InlineTok : if (instr.Operand is TypeReference) ni.Operand = context.Import ((TypeReference) instr.Operand); else if (instr.Operand is FieldReference) ni.Operand = context.Import ((FieldReference) instr.Operand); else if (instr.Operand is MethodReference) ni.Operand = context.Import ((MethodReference) instr.Operand); break; case OperandType.ShortInlineBrTarget : case OperandType.InlineBrTarget : case OperandType.InlineSwitch : break; default : ni.Operand = instr.Operand; break; } worker.Append (ni); } for (int i = 0; i < body.Instructions.Count; i++) { Instruction instr = nb.Instructions [i]; Instruction oldi = body.Instructions [i]; if (instr.OpCode.OperandType == OperandType.InlineSwitch) { Instruction [] olds = (Instruction []) oldi.Operand; Instruction [] targets = new Instruction [olds.Length]; for (int j = 0; j < targets.Length; j++) targets [j] = GetInstruction (body, nb, olds [j]); instr.Operand = targets; } else if (instr.OpCode.OperandType == OperandType.ShortInlineBrTarget || instr.OpCode.OperandType == OperandType.InlineBrTarget) instr.Operand = GetInstruction (body, nb, (Instruction) oldi.Operand); } if (!body.HasExceptionHandlers) return nb; foreach (ExceptionHandler eh in body.ExceptionHandlers) { ExceptionHandler neh = new ExceptionHandler (eh.Type); neh.TryStart = GetInstruction (body, nb, eh.TryStart); neh.TryEnd = GetInstruction (body, nb, eh.TryEnd); neh.HandlerStart = GetInstruction (body, nb, eh.HandlerStart); neh.HandlerEnd = GetInstruction (body, nb, eh.HandlerEnd); switch (eh.Type) { case ExceptionHandlerType.Catch : neh.CatchType = context.Import (eh.CatchType); break; case ExceptionHandlerType.Filter : neh.FilterStart = GetInstruction (body, nb, eh.FilterStart); neh.FilterEnd = GetInstruction (body, nb, eh.FilterEnd); break; } nb.ExceptionHandlers.Add (neh); } return nb; }