Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:53:36+00:00 2026-06-05T06:53:36+00:00

When trying to use Moq to setup an indexer property, the underlying Castle library

  • 0

When trying to use Moq to setup an indexer property, the underlying Castle library throws an InvalidCastException. I am using Moq 4.0.10827, the details follow. Thanks in advance if anyone can help me get the simple test (below) to run. I am rewriting unit tests with Moq and I am trying to re-do the test for the following method:

public INetwork GetInputNetwork(IInputPortMgr inPortMgr)
{
    var port = inPortMgr[0];
    return port.InputNetwork;
}

The IInputPortMgr interface is as follows:

public interface IInputPortMgr
{        
    IInputPort this[int index] { get; }
}

The test I wrote (and tried numerous variations) can be summarized by:

[Test]
public void GetInputNetwork_Returns_InputNetwork_From_InputPort()
{
    var mockInPortMgr = new Mock<IInputPortMgr>();
    var mockInPort = new Mock<IInputPort>();
    var mockNet = new Mock<INetwork>();

    mockInPortMgr.Setup(m => m[0]).Returns(mockInPort.Object);  // exception here
    mockInPort.Setup(m => m.InputNetwork).Returns(mockNet.Object);

    // Assertions Here
}

But when run the unit test fails because an exception is thrown by the line

mockInPortMgr.Setup(m => m[0]).Returns(mockInPort.Object);

The details of the exception are:

System.InvalidCastException : Unable to cast object of type 'System.Collections.ObjectModel.ReadOnlyCollection`1[System.Reflection.CustomAttributeTypedArgument]' to type 'System.Array'.
at System.Reflection.Emit.CustomAttributeBuilder.EmitValue(BinaryWriter writer, Type type, Object value)
at System.Reflection.Emit.CustomAttributeBuilder.InitCustomAttributeBuilder(ConstructorInfo con, Object[] constructorArgs, PropertyInfo[] namedProperties, Object[] propertyValues, FieldInfo[] namedFields, Object[] fieldValues)
at Castle.DynamicProxy.AttributeUtil.CreateBuilder(CustomAttributeData attribute)
at Castle.DynamicProxy.AttributeUtil.<GetNonInheritableAttributes>d__0.MoveNext()
at Castle.DynamicProxy.Generators.MetaProperty.BuildPropertyEmitter(ClassEmitter classEmitter)
at Castle.DynamicProxy.Contributors.CompositeTypeContributor.ImplementProperty(ClassEmitter emitter, MetaProperty property, ProxyGenerationOptions options)
at Castle.DynamicProxy.Contributors.CompositeTypeContributor.Generate(ClassEmitter class, ProxyGenerationOptions options)
at Castle.DynamicProxy.Generators.InterfaceProxyWithoutTargetGenerator.GenerateType(String typeName, Type proxyTargetType, Type[] interfaces, INamingScope namingScope)
at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors)
at Moq.Proxy.CastleProxyFactory.CreateProxy(ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
at Moq.Mock`1.<InitializeInstance>b__0()
at Moq.Mock`1.InitializeInstance()
at Moq.Mock`1.OnGetObject()
at Moq.Mock`1.get_Object()
at Tests.Psi.Type6.Fx.Type6Fixture.GetInputNetwork() in Type6Fixture.cs: line 293  

Any ideas or could anyone point me in the right direction? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:53:39+00:00Added an answer on June 5, 2026 at 6:53 am

    It’s a bug in the version of the Castle DynamicProxy that is merged into the moq binary (Castle.Core 2.5.0.0). It looks to affect attributes that accepts a params argument. I am assuming IInputPort is decorated with such an attribute since the code is not provided?

    You could compile moq with the latest version of Castle.Core (3.0.0.0) (ideal).

    Or (less ideal), download the latest moq release from Google Code, which includes a moq.dll version without Castle.Core embedded. Reference this in your project along with Castle.Core 3.0.0.0 and add the following binding redirect in your app.config. Since this is a major point upgrade I can’t vouch for it’s backward compatibility, but I tested this with your code (and an IInputPort interface decorated with an attribute that surfaces the bug in 2.5.0.0) and it worked.

      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="Castle.Core" 
                              publicKeyToken="407dd0808d44fbdc" 
                              culture="neutral" />
            <bindingRedirect oldVersion="2.5.0.0" 
                             newVersion="3.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use Moq to assign a property when a method is
I'm trying to use Moq 3.x, it works superbly. However, I have a problem
I am trying to learn how to use Moq to do unit testing with
I was trying use svn to find a checkin using svn log, but it
I am trying to use VerifySet with Moq to check the number of times
I'm trying to use an IHTMLSelectElement with Moq. My code is something like: //
I am having an issue using the Moq library to mock an Enum within
I'm trying use make a div's background transparent using a mixture of CSS3 rgba()
I'm trying to work out how this Mock (using the Moq framework) stuff all
I am trying to learn and implement TDD specifically using Moq and I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.