After some hours of debugging I just found out why generic methods
still don't work in my project but work perfectly in the NHibernate
unit tests: It's the .NET runtime version.
NUnit is using the 2.0 runtime, everything works fine there. But my
project is using the 4.0 runtime and there the call to the generic
method fails.
Luckily this can be reproduced without changing any code. If you
change the app.config files of NUnit (in my case nunit-x86.exe.config
and nunit-agent-x86.exe.config) the way it's shown here
http://mint.litemedia.se/2010/11/04/nunit-for-net-framework-4/ both
tests of GenericMethodsTests.GenericMethodShouldBeProxied fail.
The exception is "InvalidOperationException : Late bound operations
cannot be performed on types or methods for which
ContainsGenericParameters is true."
The problem here is that with the assembly generated in the 4.0
runtime the MethodInfo parameter of the InvocationInfo constructor is
an open generic method (As<T>) instead of a closed one (As<MyClass>)
and therefore the info.TargetMethod.Invoke() call of the Interceptor
fails with the exception I mentioned above.
While debugging this I compared the generated dynamic assemblies and
there I found a little difference between the assembly generated in
2.0 and 4.0:
2.0 code line:
L_0014: ldtoken instance !!0
[NHibernate.Test]NHibernate.Test.DynamicProxyTests.GenericMethodsTests.GenericMethodShouldBeProxied/
MyClass::As<!!T0>()
4.0 code line:
L_0014: ldtoken instance !!0
[NHibernate.Test]NHibernate.Test.DynamicProxyTests.GenericMethodsTests.GenericMethodShouldBeProxied/
MyClass::As()
(note the missing generic type parameter at the end)
So it seems like a fix should be made in
DefaultMethodEmitter.EmitMethodBody(), but sadly all that IL is way to
complex for me, so I stopped the debugging there.
Should I submit a Jira issue about this although I can't provide a
unit test that fails with the 2.0 runtime?
And any chance someone will look into this? :-)
On May 16, 5:07 pm, "Richard Brown \(gmail\)"