Hi,
I have come across a bug that has been on the mailing list before but no
solution was put forward:
public class Y {}
public class X<T> {
public X(T x)
{
}
}
var assDef = AssemblyFactory.DefineAssembly("test", "test",
TargetRuntime.NET_2_0, AssemblyKind.Dll);
var module = assDef.MainModule;
var ci = module.Import(typeof(X<Y>).GetConstructors()[0]);
Will return a MethodReference like this:
System.Void X`1<Y>::.ctor(Y)
But what should be emitted is:
System.Void X`1<Y>::.ctor(!0)
I think the bug is in ReflectionHelper.ImportMethodBase wioth the for loop
SR.ParameterInfo[] parameters = mb.GetParameters();
for(int i = 0; i < parameters.Length; i++)
meth.Parameters.Add(new ParameterDefinition(
ImportSystemType(parameters[i].ParameterType,
context)));
}
To get around this I made a quickfix
if(mb.DeclaringType.IsGenericType) {
bool isConstructor = mb is SR.ConstructorInfo;
SR.MethodBase[] mbList = isConstructor ?
(SR.MethodBase[])mb.DeclaringType.GetConstructors() :
mb.DeclaringType.GetMethods();
int pos = Array.IndexOf(mbList, mb);
Type openType = mb.DeclaringType.GetGenericTypeDefinition();
SR.MethodBase openMb = (isConstructor ?
(SR.MethodBase[])openType.GetConstructors() : openType.GetMethods())[pos];
SR.ParameterInfo[] parameters = openMb.GetParameters();
GenericInstanceType git =
(GenericInstanceType)meth.DeclaringType;
// this is not correct - what if the method is generic, what
if it is generic in a generic in a generic etc..
foreach(SR.ParameterInfo pi in parameters) {
Type pType = pi.ParameterType;
if(pType.IsGenericParameter) {
meth.Parameters.Add(new
ParameterDefinition(git.ElementType.GenericParameters[pType.GenericParameterPosition]));
} else {
meth.Parameters.Add(new
ParameterDefinition(ImportSystemType(pType, context)));
}
}
} else {
SR.ParameterInfo[] parameters = mb.GetParameters();
for(int i = 0; i < parameters.Length; i++)
meth.Parameters.Add(new ParameterDefinition(
ImportSystemType(parameters[i].ParameterType,
context)));
}
Is there a way to improve this? Include a better version in cecil?
regards,
Dirk
--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---