public virtual FieldReference ImportFieldReference(FieldReference fr, ImportContext context) { if (fr.DeclaringType.Module == m_module) return fr; FieldReference field = (FieldReference) GetMemberReference (fr); if (field != null) return field; field = new FieldReference ( fr.Name, ImportTypeReference (fr.DeclaringType, context), ImportTypeReference (fr.FieldType, context)); m_module.MemberReferences.Add (field); return field; }
TypeSpecification GetTypeSpec (TypeSpecification original, ImportContext context) { TypeSpecification typeSpec; TypeReference elementType = ImportTypeReference (original.ElementType, context); if (original is PointerType) { typeSpec = new PointerType (elementType); } else if (original is ArrayType) { // deal with complex arrays typeSpec = new ArrayType (elementType); } else if (original is ReferenceType) { typeSpec = new ReferenceType (elementType); } else if (original is GenericInstanceType) { GenericInstanceType git = original as GenericInstanceType; GenericInstanceType genElemType = new GenericInstanceType (elementType); context.GenericContext.CheckProvider (genElemType.GetOriginalType (), git.GenericArguments.Count); foreach (TypeReference arg in git.GenericArguments) genElemType.GenericArguments.Add (ImportTypeReference (arg, context)); typeSpec = genElemType; } else if (original is ModifierOptional) { TypeReference mt = (original as ModifierOptional).ModifierType; typeSpec = new ModifierOptional (elementType, ImportTypeReference (mt, context)); } else if (original is ModifierRequired) { TypeReference mt = (original as ModifierRequired).ModifierType; typeSpec = new ModifierRequired (elementType, ImportTypeReference (mt, context)); } else if (original is SentinelType) { typeSpec = new SentinelType (elementType); } else if (original is FunctionPointerType) { FunctionPointerType ori = original as FunctionPointerType; FunctionPointerType fnptr = new FunctionPointerType ( ori.HasThis, ori.ExplicitThis, ori.CallingConvention, new MethodReturnType (ImportTypeReference (ori.ReturnType.ReturnType, context))); foreach (ParameterDefinition parameter in ori.Parameters) fnptr.Parameters.Add (new ParameterDefinition (ImportTypeReference (parameter.ParameterType, context))); typeSpec = fnptr; } else throw new ReflectionException ("Unknown element type: {0}", original.GetType ().Name); return typeSpec; }
MethodReference ImportGenericInstanceMethod (SR.MethodInfo mi, ImportContext context) { SR.MethodInfo gmd = (SR.MethodInfo) mi.GetType ().GetMethod ("GetGenericMethodDefinition").Invoke (mi, null); GenericInstanceMethod gim = new GenericInstanceMethod ( ImportMethodBase (gmd, gmd.ReturnType, context)); foreach (Type genArg in GetGenericArguments (mi)) gim.GenericArguments.Add (ImportSystemType (genArg, context)); return gim; }
public MethodReference ImportConstructorInfo (SR.ConstructorInfo ci, ImportContext context) { return ImportMethodBase (ci, typeof (void), context); }
GenericInstanceType GetGenericType (Type t, TypeReference element, ImportContext context) { GenericInstanceType git = new GenericInstanceType (element); foreach (Type genArg in GetGenericArguments (t)) git.GenericArguments.Add (ImportSystemType (genArg, context)); return git; }
TypeReference GetTypeSpec (Type t, ImportContext context) { Stack s = new Stack (); while (t.HasElementType || IsGenericTypeSpec (t)) { s.Push (t); if (t.HasElementType) t = t.GetElementType (); else if (IsGenericTypeSpec (t)) { t = (Type) t.GetType ().GetMethod ("GetGenericTypeDefinition").Invoke (t, null); break; } } TypeReference elementType = ImportSystemType (t, context); while (s.Count > 0) { t = (Type) s.Pop (); if (t.IsPointer) elementType = new PointerType (elementType); else if (t.IsArray) elementType = new ArrayType (elementType, t.GetArrayRank ()); else if (t.IsByRef) elementType = new ReferenceType (elementType); else if (IsGenericTypeSpec (t)) elementType = GetGenericType (t, elementType, context); else throw new ReflectionException ("Unknown element type"); } return elementType; }
public MethodReference ImportMethodReference(MethodReference mr, ImportContext context) { if (mr.DeclaringType.Module == m_module) return mr; ImportCache (); if (mr is MethodSpecification) return GetMethodSpec (mr, context); MethodReference meth = m_memberRefCache [mr.ToString ()] as MethodReference; if (meth != null) return meth; meth = new MethodReference ( mr.Name, mr.HasThis, mr.ExplicitThis, mr.CallingConvention); meth.DeclaringType = ImportTypeReference (mr.DeclaringType, context); TypeReference contextType = meth.DeclaringType; while (contextType is TypeSpecification) contextType = (contextType as TypeSpecification).ElementType; context.GenericContext.Method = meth; context.GenericContext.Type = contextType; foreach (GenericParameter gp in mr.GenericParameters) meth.GenericParameters.Add (GenericParameter.Clone (gp, context)); meth.ReturnType.ReturnType = ImportTypeReference (mr.ReturnType.ReturnType, context); foreach (ParameterDefinition param in mr.Parameters) meth.Parameters.Add (new ParameterDefinition ( ImportTypeReference (param.ParameterType, context))); m_module.MemberReferences.Add (meth); m_memberRefCache [mr.ToString ()] = meth; return meth; }
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); 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); }
TypeSpecification GetTypeSpec (TypeSpecification original, ImportContext context) { TypeSpecification typeSpec; TypeReference elementType = ImportTypeReference (original.ElementType, context); if (original is PointerType) { typeSpec = new PointerType (elementType); } else if (original is ArrayType) // deal with complex arrays { typeSpec = new ArrayType (elementType); } else if (original is ReferenceType) { typeSpec = new ReferenceType (elementType); } else if (original is GenericInstanceType) { GenericInstanceType git = original as GenericInstanceType; GenericInstanceType genElemType = new GenericInstanceType (elementType); context.GenericContext.CheckProvider (genElemType.GetOriginalType (), git.GenericArguments.Count); foreach (TypeReference arg in git.GenericArguments) genElemType.GenericArguments.Add (ImportTypeReference (arg, context)); typeSpec = genElemType; } else if (original is ModifierOptional) { TypeReference mt = (original as ModifierOptional).ModifierType; typeSpec = new ModifierOptional (elementType, ImportTypeReference (mt, context)); } else if (original is ModifierRequired) { TypeReference mt = (original as ModifierRequired).ModifierType; typeSpec = new ModifierRequired (elementType, ImportTypeReference (mt, context)); } else if (original is SentinelType) { typeSpec = new SentinelType (elementType); } else if (original is FunctionPointerType) { FunctionPointerType ori = original as FunctionPointerType; FunctionPointerType fnptr = new FunctionPointerType ( ori.HasThis, ori.ExplicitThis, ori.CallingConvention, new MethodReturnType (ImportTypeReference (ori.ReturnType.ReturnType, context))); foreach (ParameterDefinition parameter in ori.Parameters) fnptr.Parameters.Add (new ParameterDefinition (ImportTypeReference (parameter.ParameterType, context))); typeSpec = fnptr; } else throw new ReflectionException ("Unknown element type: {0}", original.GetType ().Name); return typeSpec; }
TypeReference GetTypeSpec(TypeReference t, ImportContext context) { Stack s = new Stack (); while (t is TypeSpecification) { s.Push (t); t = (t as TypeSpecification).ElementType; } TypeReference elementType = ImportTypeReference (t, context); while (s.Count > 0) { t = s.Pop () as TypeReference; if (t is PointerType) elementType = new PointerType (elementType); else if (t is ArrayType) // deal with complex arrays elementType = new ArrayType (elementType); else if (t is ReferenceType) elementType = new ReferenceType (elementType); else if (t is GenericInstanceType) { GenericInstanceType git = t as GenericInstanceType; GenericInstanceType genElemType = new GenericInstanceType (elementType); foreach (TypeReference arg in git.GenericArguments) genElemType.GenericArguments.Add (ImportTypeReference (arg, context)); elementType = genElemType; } else throw new ReflectionException ("Unknown element type: {0}", t.GetType ().Name); } return elementType; }
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; }
MethodReference GetMethodSpec(MethodReference meth, ImportContext context) { if (!(meth is GenericInstanceMethod)) return null; GenericInstanceMethod gim = meth as GenericInstanceMethod; GenericInstanceMethod ngim = new GenericInstanceMethod ( ImportMethodReference (gim.ElementMethod, context)); foreach (TypeReference arg in gim.GenericArguments) ngim.GenericArguments.Add (ImportTypeReference (arg, context)); return ngim; }
static GenericParameter GetGenericParameter(GenericParameter gp, ImportContext context) { GenericParameter p; if (gp.Owner is TypeReference) p = context.GenericContext.Type.GenericParameters [gp.Position]; else if (gp.Owner is MethodReference) p = context.GenericContext.Method.GenericParameters [gp.Position]; else throw new NotSupportedException (); return p; }
public TypeDefinition ImportTypeDefinition(TypeDefinition type, ImportContext context) { return TypeDefinition.Clone (type, context); }
public FieldReference ImportFieldInfo (SR.FieldInfo fi, ImportContext context) { string sig = GetFieldSignature (fi); FieldReference f = (FieldReference) GetMemberReference (sig); if (f != null) return f; f = new FieldReference ( fi.Name, ImportSystemType (fi.DeclaringType, context), ImportSystemType (fi.FieldType, context)); m_module.MemberReferences.Add (f); return f; }
public virtual MethodReference ImportMethodReference(MethodReference mr, ImportContext context) { if (mr.DeclaringType.Module == m_module) return mr; if (mr is MethodSpecification) return GetMethodSpec (mr, context); MethodReference meth = (MethodReference) GetMemberReference (mr); if (meth != null) return meth; meth = new MethodReference ( mr.Name, mr.HasThis, mr.ExplicitThis, mr.CallingConvention); meth.DeclaringType = ImportTypeReference (mr.DeclaringType, context); TypeReference contextType = meth.DeclaringType.GetOriginalType (); context.GenericContext.Method = meth; context.GenericContext.Type = contextType; foreach (GenericParameter gp in mr.GenericParameters) meth.GenericParameters.Add (GenericParameter.Clone (gp, context)); meth.ReturnType.ReturnType = ImportTypeReference (mr.ReturnType.ReturnType, context); foreach (ParameterDefinition param in mr.Parameters) meth.Parameters.Add (new ParameterDefinition ( ImportTypeReference (param.ParameterType, context))); m_module.MemberReferences.Add (meth); return meth; }
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; }
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; }
MethodReference ImportMethodBase(SR.MethodBase mb, Type retType, ImportContext context) { if (IsGenericMethod(mb) && !IsGenericMethodDefinition(mb)) { return(ImportGenericInstanceMethod((SR.MethodInfo)mb, context)); } ImportCache(); Type originalDecType = mb.DeclaringType; Type declaringTypeDef = originalDecType; while (IsGenericTypeSpec(declaringTypeDef)) { declaringTypeDef = GetGenericTypeDefinition(declaringTypeDef); } if (mb.DeclaringType != declaringTypeDef && mb is SR.MethodInfo) { int mt = GetMetadataToken(mb as SR.MethodInfo); // hack to get the generic method definition from the constructed method foreach (SR.MethodInfo mi in declaringTypeDef.GetMethods()) { if (GetMetadataToken(mi) == mt) { mb = mi; retType = mi.ReturnType; break; } } } string sig = GetMethodBaseSignature(mb, originalDecType, retType); MethodReference meth = m_memberRefCache [sig] as MethodReference; if (meth != null) { return(meth); } meth = new MethodReference( mb.Name, (mb.CallingConvention & SR.CallingConventions.HasThis) > 0, (mb.CallingConvention & SR.CallingConventions.ExplicitThis) > 0, MethodCallingConvention.Default); // TODO: get the real callconv meth.DeclaringType = ImportSystemType(originalDecType, context); if (IsGenericMethod(mb)) { foreach (Type genParam in GetGenericArguments(mb as SR.MethodInfo)) { meth.GenericParameters.Add(new GenericParameter(genParam.Name, meth)); } } context.GenericContext.Method = meth; context.GenericContext.Type = ImportSystemType(declaringTypeDef, context); meth.ReturnType.ReturnType = ImportSystemType(retType, context); SR.ParameterInfo [] parameters = mb.GetParameters(); for (int i = 0; i < parameters.Length; i++) { meth.Parameters.Add(new ParameterDefinition( ImportSystemType(parameters [i].ParameterType, context))); } m_module.MemberReferences.Add(meth); m_memberRefCache [sig] = meth; return(meth); }
public MethodDefinition ImportMethodDefinition(MethodDefinition meth, ImportContext context) { return MethodDefinition.Clone (meth, context); }
internal static CustomAttribute Clone (CustomAttribute custattr, ImportContext context) { CustomAttribute ca = new CustomAttribute (context.Import (custattr.Constructor)); custattr.CopyTo (ca); return ca; }
public MethodReference ImportConstructorInfo(SR.ConstructorInfo ci, ImportContext context) { return(ImportMethodBase(ci, typeof(void), context)); }
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; }
public MethodReference ImportMethodInfo(SR.MethodInfo mi, ImportContext context) { return(ImportMethodBase(mi, mi.ReturnType, context)); }
static GenericParameter GetGenericParameter(Type t, ImportContext context) { int pos = (int) t.GetType ().GetProperty ("GenericParameterPosition").GetValue (t, null); if (GenericParameterOfMethod (t)) return context.GenericContext.Method.GenericParameters [pos]; else return context.GenericContext.Type.GenericParameters [pos]; }
public FieldDefinition ImportFieldDefinition(FieldDefinition field, ImportContext context) { return(FieldDefinition.Clone(field, context)); }
static GenericParameter GetGenericParameter (Type t, ImportContext context) { int pos = (int) t.GetType ().GetProperty ("GenericParameterPosition").GetValue (t, null); IGenericParameterProvider provider; if (GenericParameterOfMethod (t)) provider = context.GenericContext.Method; else provider = context.GenericContext.Type; if (provider == null) throw new InvalidOperationException ("Invalid context"); return provider.GenericParameters [pos]; }
public MethodDefinition ImportMethodDefinition(MethodDefinition meth, ImportContext context) { return(MethodDefinition.Clone(meth, context)); }
public TypeReference ImportSystemType (Type t, ImportContext context) { if (t.HasElementType || IsGenericTypeSpec (t)) return GetTypeSpec (t, context); if (IsGenericParameter (t)) return GetGenericParameter (t, context); TypeReference type = m_module.TypeReferences [GetTypeSignature (t)]; if (type != null) return AdjustReference (t, type); AssemblyNameReference asm = ImportAssembly (t.Assembly); if (t.DeclaringType != null) { type = new TypeReference (t.Name, string.Empty, asm, t.IsValueType); type.DeclaringType = ImportSystemType (t.DeclaringType, context); } else type = new TypeReference (t.Name, t.Namespace, asm, t.IsValueType); if (IsGenericTypeDefinition (t)) foreach (Type genParam in GetGenericArguments (t)) type.GenericParameters.Add (new GenericParameter (genParam.Name, type)); context.GenericContext.Type = type; m_module.TypeReferences.Add (type); return type; }
public TypeDefinition ImportTypeDefinition(TypeDefinition type, ImportContext context) { return(TypeDefinition.Clone(type, context)); }
MethodReference ImportMethodBase (SR.MethodBase mb, Type retType, ImportContext context) { if (IsGenericMethod (mb) && !IsGenericMethodDefinition (mb)) return ImportGenericInstanceMethod ((SR.MethodInfo) mb, context); Type originalDecType = mb.DeclaringType; Type declaringTypeDef = originalDecType; while (IsGenericTypeSpec (declaringTypeDef)) declaringTypeDef = GetGenericTypeDefinition (declaringTypeDef); if (mb.DeclaringType != declaringTypeDef && mb is SR.MethodInfo) { int mt = GetMetadataToken (mb as SR.MethodInfo); // hack to get the generic method definition from the constructed method foreach (SR.MethodInfo mi in declaringTypeDef.GetMethods ()) { if (GetMetadataToken (mi) == mt) { mb = mi; retType = mi.ReturnType; break; } } } string sig = GetMethodBaseSignature (mb, originalDecType, retType); MethodReference meth = (MethodReference) GetMemberReference (sig); if (meth != null) return meth; meth = new MethodReference ( mb.Name, (mb.CallingConvention & SR.CallingConventions.HasThis) > 0, (mb.CallingConvention & SR.CallingConventions.ExplicitThis) > 0, MethodCallingConvention.Default); // TODO: get the real callconv meth.DeclaringType = ImportSystemType (originalDecType, context); if (IsGenericMethod (mb)) foreach (Type genParam in GetGenericArguments (mb as SR.MethodInfo)) meth.GenericParameters.Add (new GenericParameter (genParam.Name, meth)); TypeReference contextType = context.GenericContext.Type; MethodReference contextMethod = context.GenericContext.Method; context.GenericContext.Method = meth; context.GenericContext.Type = ImportSystemType (declaringTypeDef, context); meth.ReturnType.ReturnType = ImportSystemType (retType, context); SR.ParameterInfo [] parameters = mb.GetParameters (); for (int i = 0; i < parameters.Length; i++) meth.Parameters.Add (new ParameterDefinition ( ImportSystemType (parameters [i].ParameterType, context))); context.GenericContext.Type = contextType; context.GenericContext.Method = contextMethod; m_module.MemberReferences.Add (meth); return meth; }
internal static void CloneInto(IGenericParameterProvider old, IGenericParameterProvider np, ImportContext context) { foreach (GenericParameter gp in old.GenericParameters) { GenericParameter ngp = Clone(gp, context); np.GenericParameters.Add(ngp); CloneConstraints(gp, ngp, context); } }
public MethodReference ImportMethodInfo (SR.MethodInfo mi, ImportContext context) { return ImportMethodBase (mi, mi.ReturnType, context); }
public virtual TypeReference ImportTypeReference(TypeReference t, ImportContext context) { if (t.Module == m_module) return t; if (t is TypeSpecification) return GetTypeSpec (t as TypeSpecification, context); if (t is GenericParameter) return GetGenericParameter (t as GenericParameter, context); TypeReference type = m_module.TypeReferences [t.FullName]; if (type != null) return type; AssemblyNameReference asm; if (t.Scope is AssemblyNameReference) asm = ImportAssembly ((AssemblyNameReference) t.Scope); else if (t.Scope is ModuleDefinition) asm = ImportAssembly (((ModuleDefinition) t.Scope).Assembly.Name); else throw new NotImplementedException (); if (t.DeclaringType != null) { type = new TypeReference (t.Name, string.Empty, asm, t.IsValueType); type.DeclaringType = ImportTypeReference (t.DeclaringType, context); } else type = new TypeReference (t.Name, t.Namespace, asm, t.IsValueType); context.GenericContext.Type = type; foreach (GenericParameter gp in t.GenericParameters) type.GenericParameters.Add (GenericParameter.Clone (gp, context)); m_module.TypeReferences.Add (type); return type; }
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; }
protected static GenericParameter GetGenericParameter(GenericParameter gp, ImportContext context) { // walk generic context stack, looking for the generic parameter GenericParameter p = null; context.SearchGenericContextStack(delegate(GenericContext genericContext) { if (gp.Owner is TypeReference && genericContext.Type.GenericParameters.Count > gp.Position) { p = genericContext.Type.GenericParameters[gp.Position]; return true; } else if (gp.Owner is MethodReference && genericContext.Method.GenericParameters.Count > gp.Position) { p = genericContext.Method.GenericParameters[gp.Position]; return true; } else { return false; } }); if (p == null) { throw new NotSupportedException("Unable to find generic parameter " + gp + " in context " + context); } return p; }
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; }
protected MethodReference GetMethodSpec(MethodReference meth, ImportContext context) { if (!(meth is GenericInstanceMethod)) return null; GenericInstanceMethod gim = meth as GenericInstanceMethod; GenericInstanceMethod ngim = new GenericInstanceMethod ( ImportMethodReference (gim.ElementMethod, context)); context.GenericContext.CheckProvider (ngim.GetOriginalMethod (), gim.GenericArguments.Count); foreach (TypeReference arg in gim.GenericArguments) ngim.GenericArguments.Add (ImportTypeReference (arg, context)); return ngim; }