InternalsVisibleTo("DynamicProxyGenAssembly2") not working for internal types

4,577 views
Skip to first unread message

Brian Noyes

unread,
Sep 24, 2010, 12:10:42 PM9/24/10
to Moq Discussions
I'm using the latest build of MOQ ( 4.0.10827) and am trying to mock
an internal interface in a Silverlight 4 class library and the trick I
have found everywhere of using
InternalsVisibleTo("DynamicProxyGenAssembly2") is not working for me.
I've replicated it in a very simple project.

I have a class in a SL4 class library to test that is internal with an
internal interface:
internal interface IClass1
{
void DoWork();
}
internal class Class1 : IClass1
{

public void DoWork()
{
throw new NotImplementedException();
}
}

I have the following in the AssemblyInfo.cs of that class library
project:
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
[assembly: InternalsVisibleTo("SilverlightTest1")]

(the name of the SLUT project is SilverlightTest1)

The test is simply:
[TestClass]
public class Tests
{
[TestMethod]
public void TestMethod1()
{
var mock = new Mock<IClass1>();
var local = mock.Object;
}
}

When dereferencing the Object, the following exception is thrown;
GeneratorException: Type MOQInternalsTest.IClass1is not public. Can
not create proxy for types that are not accessible.

What am I missing here?
Thanks, Brian

Daniel Cazzulino

unread,
Sep 24, 2010, 2:47:10 PM9/24/10
to moq...@googlegroups.com
From the http://code.google.com/p/moq/wiki/QuickStart, Advanced section:

  • Mocking internal types of another project: add the following assembly attribute (typically to the AssemblyInfo.cs) to the project containing the internal types:
  • 
    // This assembly is the default dynamic assembly generated Castle DynamicProxy, 
    // used by Moq. Paste in a single line.
    [assembly:InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]


/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


Brian Noyes

unread,
Sep 28, 2010, 10:13:54 AM9/28/10
to Moq Discussions
Hmmm, even with the fully qualified version you show here, I still get
the same error.

On Sep 24, 2:47 pm, Daniel Cazzulino <dan...@cazzulino.com> wrote:
> From thehttp://code.google.com/p/moq/wiki/QuickStart, Advanced section:
>
>    - Mocking internal types of another project: add the following assembly
>    attribute (typically to the AssemblyInfo.cs) to the project containing the
>    internal types:
>
>    // This assembly is the default dynamic assembly generated Castle
> DynamicProxy,
>    // used by Moq. Paste in a single line.
>    [assembly:InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004­800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225­ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753c­c297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46a­d2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e9248­4cf7045cc7")]
>
> /kzu
>
> --
> Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1
> 425.329.3471
>
> > Unsubscribe: moqdisc-u...@googlegroups.com- Hide quoted text -
>
> - Show quoted text -

Daniel Cazzulino

unread,
Sep 28, 2010, 11:40:28 AM9/28/10
to moq...@googlegroups.com
Koz, has any of this changed in the new Castle.Core.dll ?


/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471



Reply all
Reply to author
Forward
0 new messages