PrivateObject不找物业 [英] PrivateObject does not find property

查看:169
本文介绍了PrivateObject不找物业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构,它看起来basicly是这样的:

I have a structure which looks basicly like this:

abstract class A
{
   protected string Identificator { get; set; }

   private void DoSomething()
   {

       // ...

       DoSomethingSpecific();
   }

   protected abstract void DoSomethingSpecific();
}

由于的,我需要做单元测试的复杂性的DoSomething的方法,以确保它的工作原理永诺以同样的方式。这就是为什么我在下面的残余部分。

Because of the complexity I need do unit tests the DoSomething method to be sure it works allways in the same way. Thats why I created following stub.

public class AStub : A
{
    protected override void DoSomethingSpecific()
    {
        // nothing to do
    }
}

我用的是PrivateObject类访问方法和类A的属性来实例化类AStub。这工作了一段时间,由于某种原因崩溃了,现在每当我试图访问的财产,或方法。

I use the PrivateObject class to access the methods and properties of class A be instantiating class AStub. This worked for a while and for some reason crashes now whenever I try to access either the property or the method.

以下code来进行测试:

following code for testing:

var sut = new CommonIodAdapterImpl();
var accessor = new PrivateObject(sut);

accessor.SetProperty("Identificator", "blablub");
accessor.Invoke("DoSomething", null);

// assert...

这是引发的异常是的MissingMethodException告诉我,propertie或方法没有被发现。但是,当我调试,检查每一个层次结构似乎是正确的inclduing拼写。

The exception which is thrown is a MissingMethodException telling me that the propertie or method was not found. But when I debug and check the hierachy every seems to be right inclduing the spelling.

感谢您的帮助。

推荐答案

您需要将PrivateType参数设置为你的基类来访问私有成员在这一水平。

You need to set the PrivateType argument to your base class to access the private members at that level.

var accessor = new PrivateObject(sut, new PrivateType(typeof(A)));

这篇关于PrivateObject不找物业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆