Dynamic mock of a generics interface that uses a generic constraint

380 views
Skip to first unread message

Yan Décarie

unread,
Jul 30, 2009, 10:06:36 AM7/30/09
to Rhino.Mocks
We have encountered some problem with the use of Rhino Mock in .Net
when trying to dynamically mock a generics interface that uses a
generic constraint.

Here is the interface definition:

public interface IClonable<TU>
{
T Clone<T>() where T : TU;
}

We’ve got an interface that derives from it that looks like :

public interface IObjectX: IClonable<IObjectX>

When I create this interface in rhino mocks

mocksRepository.DynamicMock<IObjectX>()

It crashes with the following error:

[...] tried to implicitly implement an interface method with weaker
type parameter constraints

If I remove the generic TU, or the constraint, it works but being a
framework class I'd rather for it to generate the error at compile
time instead of run time.

Are you aware of this problem and is there a way to avoid it or as it
been specifically fix in 3.5? We currently are using 3.4 and while
looking at your release notes for 3.5 it did not seem to be fixed.

Here is the actual stack leading to the error. It might be helpful to
you.

at System.Reflection.Emit.TypeBuilder._TermCreateClass(Int32 handle,
Module module)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at
Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType
()
at
Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode
(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions
options)
at
Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget
(Type theInterface, Type[] interfaces, ProxyGenerationOptions options)
at
Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyTypeWithoutTarget
(Type theInterface, Type[] interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget
(Type theInterface, Type[] interfaces, ProxyGenerationOptions options,
IInterceptor[] interceptors)
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget
(Type theInterface, Type[] interfaces, IInterceptor[] interceptors)
at Rhino.Mocks.MockRepository.MockInterface(CreateMockState
mockStateFactory, Type type, Type[] extras)
at Rhino.Mocks.MockRepository.CreateMockObject(Type type,
CreateMockState factory, Type[] extras, Object[]
argumentsForConstructor)
at Rhino.Mocks.MockRepository.DynamicMock<T>(Object[]
argumentsForConstructor)

Any help would be appreciated.
Thanks

Kenneth Xu

unread,
Jul 30, 2009, 2:36:46 PM7/30/09
to Rhino...@googlegroups.com
On Thu, Jul 30, 2009 at 10:06 AM, Yan Décarie<yan.d...@gmail.com> wrote:
> It crashes with the following error:
>
> [...] tried to implicitly implement an interface method with weaker
> type parameter constraints

This sounds like a DP issue that was fixed. I have a build of castle
DP trunk r1247 and RM r2212. Your test case passed fine under that.

Yan Décarie

unread,
Jul 30, 2009, 4:03:50 PM7/30/09
to Rhino.Mocks
I tried with the 3.5.0.0 version of Rhino Mocks and still I get the
error.

Here is the full line with the error and assembly.

System.TypeLoadException: Method 'Clone' on type 'XXX' from assembly
'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null' tried to implicitly implement an interface method
with weaker type parameter constraints.

Unless the 3.5 version does not include the build of DP and RM you
mentioned.

Kenneth Xu

unread,
Jul 30, 2009, 4:15:46 PM7/30/09
to Rhino...@googlegroups.com
Not, it doesn't.

Krzysztof fixed a number of DP issues recently. I happened to
encounter a few of them so I had to built a version of RM (RhinoMocks)
myself with those fixes.

You can check out the latest DP and RM from SVN repository to create a
build, or you'll need to wait for next release.

Yan Décarie

unread,
Jul 30, 2009, 4:41:43 PM7/30/09
to Rhino.Mocks
Thanks for the help. Is there a planed release for this year?

On Jul 30, 4:15 pm, Kenneth Xu <kenne...@gmail.com> wrote:
> Not, it doesn't.
>
> Krzysztof fixed a number of DP issues recently. I happened to
> encounter a few of them so I had to built a version of RM (RhinoMocks)
> myself with those fixes.
>
> You can check out the latest DP and RM from SVN repository to create a
> build, or you'll need to wait for next release.
>

Tuna Toksoz

unread,
Jul 30, 2009, 5:23:30 PM7/30/09
to Rhino...@googlegroups.com
Sounds like a place where horn would just work.


Kenneth Xu

unread,
Jul 30, 2009, 5:34:13 PM7/30/09
to Rhino...@googlegroups.com
On Thu, Jul 30, 2009 at 4:41 PM, Yan Décarie<yan.d...@gmail.com> wrote:
> Thanks for the help. Is there a planed release for this year?

I myself is a user of RM, so I'll leave it committers of RM and DP to
answer your questions :)

Krzysztof Koźmic

unread,
Aug 2, 2009, 5:47:37 PM8/2/09
to Rhino.Mocks
Kenneth,

Did you verify if that issue is no longer present in trunk version of
DP?

as for the release, we do plan to have a v2.2 beta probably by the end
of this month, and a release later this year.

Dunno about RM, but they'll probably wait until DP v2.2 gets released

Kenneth Xu

unread,
Aug 2, 2009, 11:35:28 PM8/2/09
to Rhino...@googlegroups.com
Krzysztof,

Didn't test with the latest trunk. My build was based on DP r1247,
which is working well for my tests. And yes, the test case passed
without issue.

Cheers,
Kenneth

2009/8/2 Krzysztof Koźmic <krzyszto...@gmail.com>:

Krzysztof Koźmic (2)

unread,
Aug 3, 2009, 6:47:05 AM8/3/09
to Rhino.Mocks
Cool.
Than It's almost certain it passes on the trunk as well.

Thanks,

Krzysztof

On Aug 3, 5:35 am, Kenneth Xu <kenne...@gmail.com> wrote:
> Krzysztof,
>
> Didn't test with the latest trunk. My build was based on DP r1247,
> which is working well for my tests. And yes,  the test case passed
> without issue.
>
> Cheers,
> Kenneth
>
> 2009/8/2 Krzysztof Koźmic <krzysztof.koz...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages