System.TypeLoadException when mocking a generic interface

139 views
Skip to first unread message

Michiel

unread,
Jan 28, 2009, 5:34:35 AM1/28/09
to Moq Discussions
I ran into the System.TypeLoadException when trying to run a test
where I mocked an IRepository<T> which has a method with the following
signature:

IQueryable<U> Find<U>() where U : T;

The error message is:

"Method 'Find' on type
'IRepository`1Proxy4ddcacc4e8f34278bbf4ebe7f1335b97' from assembly
'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null' tried to implicitly implement an interface method
with weaker type parameter constraints."

I understand that this is a bug that will be resolved in the future
(for more information see:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290053).
But still, I'd like to be able to mock my repositories today. What
would be a work around short of not having that method signature in
the interface?

Thanks for looking!

-Michiel

Daniel Cazzulino

unread,
Jan 28, 2009, 12:10:35 PM1/28/09
to moq...@googlegroups.com
well, the thing is apparently (from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282829) that not only you will NOT be able to mock, but even use such a type declaration at all.

Have you verified that you can create an actual instance of a class implementing such an interface and it actually loads in the runtime? According to the connect bug, that's unlikely...
--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471

Michiel

unread,
Jan 28, 2009, 2:15:15 PM1/28/09
to Moq Discussions
The Rhino.Mocks project had a similar problem, and they got it fixed
somehow:

http://groups.google.com/group/RhinoMocks/msg/b1d175d2424a9bb1

In the notes for the 3.0 release:

"Using Dynamic Proxy 2 - better performance, better code-base, full
support for weird generic scenarios."

"Improved support for wacky generics scenarios."

From: http://ayende.com/Blog/archive/2007/03/28/Rhino-Mocks-3.0-Released.aspx

I'm not sure if Moq could work around this issue or not.

Thanks for looking either way!

-M

On Jan 28, 6:10 pm, Daniel Cazzulino <kzu....@gmail.com> wrote:
> well, the thing is apparently (fromhttps://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx...)
> that not only you will NOT be able to mock, but even use such a type
> declaration at all.
>
> Have you verified that you can create an actual instance of a class
> implementing such an interface and it actually loads in the runtime?
> According to the connect bug, that's unlikely...
>
>
>
> On Wed, Jan 28, 2009 at 8:34 AM, Michiel <michiel...@gmail.com> wrote:
>
> > I ran into the System.TypeLoadException when trying to run a test
> > where I mocked an IRepository<T> which has a method with the following
> > signature:
>
> > IQueryable<U> Find<U>() where U : T;
>
> > The error message is:
>
> > "Method 'Find' on type
> > 'IRepository`1Proxy4ddcacc4e8f34278bbf4ebe7f1335b97' from assembly
> > 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral,
> > PublicKeyToken=null' tried to implicitly implement an interface method
> > with weaker type parameter constraints."
>
> > I understand that this is a bug that will be resolved in the future
> > (for more information see:
>
> >https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx...
> > ).

Daniel Cazzulino

unread,
Jan 28, 2009, 3:47:55 PM1/28/09
to moq...@googlegroups.com
maybe we should update our DP2 drop?

Michiel

unread,
Jan 29, 2009, 4:48:06 AM1/29/09
to Moq Discussions
That's what I was thinking, assuming that it contains the fix.

I created an issue (127) with a test case:
http://code.google.com/p/moq/issues/detail?id=127

Daniel Cazzulino

unread,
Jan 29, 2009, 7:32:25 AM1/29/09
to moq...@googlegroups.com

Thanks a bunch!

/kzu from android mobile

On Jan 29, 2009 7:48 AM, "Michiel" <michi...@gmail.com> wrote:


That's what I was thinking, assuming that it contains the fix.

I created an issue (127) with a test case:
http://code.google.com/p/moq/issues/detail?id=127

On 28 jan, 21:47, Daniel Cazzulino <kzu....@gmail.com> wrote: > maybe we should update our DP2 dro...

> On Wed, Jan 28, 2009 at 5:15 PM, Michiel <michiel...@gmail.com> wrote: > > > The Rhino.Mocks proje...

Reply all
Reply to author
Forward
0 new messages