Accessing a Private Constructor from Outside the Class in C#

前端 未结 8 1596
梦如初夏
梦如初夏 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.

    0 讨论(0)
  • 2020-12-30 22:09

    The method Bobby is still in a different class, called Fred. That is why you can't access the prive constructor of the Bob class. What you are trying to do is not possible with attached methods. Even if they can be attached on another class they are still declared outside that class and follow the usual scope/access rules.

    0 讨论(0)
  • 2020-12-30 22:13

    There are several ways around this issue:

    One: Make the constructor public. If you need to access it from outside the class why is it private (it might be that you only want to access the private constructor for testing, in which case this is a valid issue).

    Two: Make the constructor protected, then access it through a derived class:

    public class Bob
    {
        public String Surname { get; set; }
    
        protected Bob()
        { }
    
        public Bob(string surname)
        {
            Surname = surname;
        }
    }
    
    public class Fred : Bob
    {
        public Fred()
            : base()
        {
        }
    }
    

    Three: Use reflection (as shown by jgauffin).

    0 讨论(0)
  • 2020-12-30 22:13

    In additional to @jgauffin's answer which tells how to call private constructors via reflection:

    Is it possible to change the modifiers of the private constructor?

    It seems that you are implementing Factory Pattern in your code. Thus the modifier is supposed to be internal.

    public class Product
    {
       //others can't create instances directly outside the assembly
       internal Product() { }    
    }
    public class ProductProvider
    {
       //they can only get standardized products by the provider
       //while you have full control to Product class inside ProductProvider
       public static Product CreateProduct()
       {
           Product p = new Product();    
           //standardize the product
           return p;
       }  
    }
    

    Extension methods

    public static MyExt
    {
       public static void DoSomething(this Product p) { }
    }
    

    Calling p.DoSomething() actually equals to MyExt.DoSomething(p). It's not putting this method into class Product.

    0 讨论(0)
  • 2020-12-30 22:15
    public class demo
    {
       private demo()
        {
            Console.WriteLine("This is no parameter private constructor");
        }
        public demo(int a)
        {
            demo d = new demo('c');// u can call both private contstructors from here
            demo dd = new demo();
            Console.WriteLine("This is one parameter public constructor");
        }
        private demo(char a)
        {
            Console.WriteLine("This is one parameter public constructor::" + a);
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            demo obj = new demo(7);
            // demo obj = new demo();  // it will raise error
            Console.ReadLine();
        }
    }
    
    0 讨论(0)
  • 2020-12-30 22:17

    You can instantiate instances of that type via reflection.

    0 讨论(0)
提交回复
热议问题