How to create an object instance of class with internal constructor via reflection?

前端 未结 2 1649
陌清茗
陌清茗 2021-01-18 11:17

Example:

class Program
{
    static void Main(string[] args)
    {
        var myClass = Activator.CreateInstance(typeof(MyClass));
    }
}
         


        
相关标签:
2条回答
  • 2021-01-18 11:51

    It is not that impossible. you've to tell it is not a public.

    var myClass = Activator.CreateInstance(typeof(MyClass), true);//say nonpublic
    
    0 讨论(0)
  • 2021-01-18 12:07

    The constructor inside your MyClass is Internal try to change to public

    public class MyClass
    {
        public MyClass()
        {
        }
    }
    

    or

    Pass true to CreateInstance

    var myClass = Activator.CreateInstance(typeof(MyClass),true );
    
    0 讨论(0)
提交回复
热议问题