从C#之外的类访问私有构造函数 [英] Accessing a Private Constructor from Outside the Class in C#

查看:247
本文介绍了从C#之外的类访问私有构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我定义了一个具有私有默认构造函数和具有参数的公共构造函数的类,我如何访问私有构造函数?

  public class Bob 
{
public String Surname {get;组; }

private Bob()
{}

public Bob(string surname)
{
Surname = surname;
}
}



我可以通过静态方法访问私有构造函数类如下:

  public static Bob GetBob()
{
return new Bob
}



我认为我可以通过扩展方法访问私有构造函数,因为根据我的理解),扩展方法被翻译,以便他们 看起来是类的静态方法,但我不能:

  static class Fred 
{
public static Bob Bobby(this Bob bob)
{
return new Bob ;因此,如何访问私有构造函数? / p>

谢谢






编辑:



我想这样做的原因是我想为我们的一个业务类创建测试,但不允许这个类的消费者能够不正确地实例化一个对象。我测试它,所以我知道(我希望!)在什么情况下测试将失败。我现在仍然是一个测试n00b,所以我的想法可能是也可能不是做错事的方式。



我改变了我的测试策略只是做事情的方式这个类的消费者,即调用公共方法,如果公共方法是OK,假设私有方法是OK。我仍然更喜欢测试私人方法,但我的老板呼唤我的脖子上交付: - (

解决方案

默认构造函数是私有的,因为开发者不会将其设为私有的。



但是如果你仍然想要使用默认的构造函数,你可以使用反射来获得它。

  var constructor = typeof Bob).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,null,new Type [0],null); 
var instance =(Bob)constructor.Invoke(null);



修改



不要测试受保护的或私有的方法/属性。如果你不能通过公共API测试这些方法/属性,你可能做错了,可以删除它们或重构类。



编辑2



忘记绑定标志。


If I define a class with a private default constructor and a public constructor that has parameters, how can I access the private constructor?

public class Bob
{
   public String Surname { get; set; }

   private Bob()
   { }

   public Bob(string surname)
   {
      Surname = surname;
   }
}

I can access the private constructor via a static method on the class like this:

public static Bob GetBob()
{
   return new Bob();
}

I thought that I could access the private constructor via an extension method, since (according to my understanding) extension methods are translated so that they appear to be static methods on the class, but I can't:

static class Fred
{
   public static Bob Bobby(this Bob bob)
   {
      return new Bob();
   }
}

So, how can I access the private constructor?

Thank you


EDIT:

The reason that I wanted to do this was that I wanted to create tests for one of our business classes, but not allow a consumer of this class to be able to instantiate an object incorrectly. I'm testing it, so I know (I hope!) under what circumstances the tests will fail. I'm still a testing n00b right now so my idea may or may not have been the "wrong way" of doing things.

I've changed my testing strategy to just do things the way the a consumer of this class would, i.e. calling the public methods and if the public methods are OK, assuming that the private methods are OK. I would still prefer to test the private methods, but my boss is breathing down my neck on a deliverable :-(

解决方案

Default constructors are private for a reason. The developer doesn't make it private for fun.

But if you still want to use the default constructor you get it by using reflection.

var constructor = typeof(Bob).GetConstructor(BindingFlags.NonPublic|BindingFlags.Instance, null, new Type[0], null);
var instance = (Bob)constructor.Invoke(null);

Edit

I saw your comment about testing. Never test protected or private methods / properties. You have probably done something wrong if you can't manage to test those methods/properties through the public API. Either remove them or refactor the class.

Edit 2

Forgot a binding flag.

这篇关于从C#之外的类访问私有构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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