Consider Following class
class A<T> where T:new()
{
public static T Instance = new T();
A()
{
}
}
I have 2 questions
-
I need
Instanceobject with Reflection. I have tried followingvar type = typeof(A<int>); // var type = typeof(A<>).MakeGenericType(typeof(int)); // Also tried this var instanceMember1 = type.GetMember("Instance", BindingFlags.Static ); // returns null var instanceMember2 = type.GetField("Instance", BindingFlags.Static ); // returns nullI have also tried to Change
Instanceto property and callGetPropertywith no success. -
After removing
new()constraint and makingconstructorprivate, How to invoke private (parameterless) constructor through reflection.
Add
BindingFlags.Publicto your flags forGetField.To invoke the private constructor: