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
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.
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.
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.
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.
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"}); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With