Dirk Bonne
unread,Oct 29, 2009, 11:59:51 AM10/29/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mono-...@googlegroups.com
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