Accessing a Private Constructor from Outside the Class in C#

前端 未结 8 1693
梦如初夏
梦如初夏 2020-12-30 21:52

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 Bo         


        
8条回答
  •  忘掉有多难
    2020-12-30 22:09

    If you're using dotnet core you can do the following without even having to fiddle with any reflection:

    YourCustomObject player = (YourCustomObject)Activator.CreateInstance(typeof(YourCustomObject),true);
    

    Activator is just in the System namespace.

提交回复
热议问题