chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi all,
I'm getting a System.NullReferenceException while trying to mock a mixed wrapper class.
The wrapper class has a private constructor. The constructor has only one parameter, a pointer to the unmanaged wrapped object. Something like this:

_gc public class Wrapper
{
  private:
    Wrapper(MyUnmanagedClass__nogc *ptr);
    [...]
}


The exception is raised when executing the following statement in the setup of a fixture:

  MockObject mockWrapper = MockManager.MockObject(typeof (Wrapper));


Is this a bug or a not supported features? Is there any fix/workaround for this problem?

Thank you

P.S.: Mocking the type doesn't raise any exception (using MockManager.Mock() method)
asked by Deimos (4.7k points)

2 Answers

0 votes
Hi,
This is not supported.

You might be able to do this yourself.
If you have access to creating the Wrapper use:
Mock mockWrapper = MockManager.Mock(typeof (Wrapper));
Wrapper wrapper = new Wrapper(dummyUnmanagedClass);

The above code is equivelant to using MockObject.

If you don't have access you will have to do this in reflection.
I am not sure how you pass an unmanaged class in reflection, I would really like to hear how if you know. But it should look like this:
Activator.CreateInstance(typeof(Wrapper),       
BindingFlags.Instance | BindingFlags.NonPublic, null, args, null, null);

I just don't know how to put dummyUnmanagedClass in the args.
answered by scott (32k points)
0 votes
Hi Scott,
thank you for the reply.
After some tries, it seems that the mixed constructor isn't reflected. Also, checking the code with Reflector shows that the constructor is marked as unsafe.
So, as a workaround, I added a default constructor, and it worked.

Thank you again
answered by Deimos (4.7k points)
...