Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating Internal class with private constructor

Tags:

c#

I am trying to use reflection to create instance of a class. But it is sealed internal and has private constructor. I wonder how can i initiaise it and as its part of framework, I can only use reflection to take it out?

internal sealed class ABC {     private ABC(string password){}     public static ABC Create(string password){}; } 

Added: System.ServiceModel.Channels.SelfSignedCertificate is the internal class I am trying to use

like image 465
sunny Avatar asked Jan 07 '10 19:01

sunny


People also ask

How do you instantiate a class with a private constructor?

Class-level Member (Lazy Initialization Method):First, initiate a constructor as private. Then create a private static instance of this singleton class. Keep in mind to NOT instantiate it. Then, write a static method, which checks the static instance member for null and initiates the instance.

How do you instantiate an internal class?

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.

Can we declare private constructor in class?

Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class. We can use this private constructor in the Singleton Design Pattern.

How do I invoke a private constructor?

Class. getDeclaredConstructor() can be used to obtain the constructor object for the private constructor of the class. The parameter for this method is a Class object array that contains the formal parameter types of the constructor.


1 Answers

EDIT: I hadn't noticed that you mentioned that the type you're trying to initialize is part of the .NET framework. I thought it was one of your own types, just referenced from elsewhere.

I would strongly urge you not to attempt this. Microsoft are perfectly at liberty to change or remove internal classes between framework releases - your code will be incredibly brittle if you rely on implementation details like this.

Change your design to avoid needing to do this.


Original answer:

Yes, you'd have to use reflection - like this:

using System; using System.Reflection;  internal sealed class ABC {     private ABC(string password)     {         Console.WriteLine("Constructor called");     } }  public class Test {     static void Main()     {         ConstructorInfo ctor = typeof(ABC).GetConstructors             (BindingFlags.Instance | BindingFlags.NonPublic)[0];          ABC abc = (ABC) ctor.Invoke(new object[] { "test" });     } } 

Note that violating access modifiers in this way requires the ReflectionPermissionFlag.MemberAccess permission. If you know there will be a static method called Create, you'd be better off invoking that via reflection:

using System; using System.Reflection;  internal sealed class ABC {     private ABC(string password)     {         Console.WriteLine("Constructor called");     }      public static ABC Create(string password)     {         return new ABC(password);     } }  public class Test {     static void Main()     {         MethodInfo method = typeof(ABC).GetMethod("Create",             BindingFlags.Static | BindingFlags.Public);          ABC abc = (ABC) method.Invoke(null, new object[]{"test"});     } } 
like image 142
Jon Skeet Avatar answered Sep 28 '22 04:09

Jon Skeet