C# Reflection: Instantiate an object with string class name

后端 未结 6 1950
礼貌的吻别
礼貌的吻别 2020-12-30 10:22

my situation is the next: I\'m working with Visual C# 2010 express developing a Windows Forms Application. When the user logins, dinamically build a menustrip with options l

6条回答
  •  感动是毒
    2020-12-30 11:19

    Its too late in the thread to answer, but the earlier answer, in current .NET framework (4.7), not working (The line Assembly assembly = Assembly.Load("AccountingSA"); always throws FileIOException). Currently, working code is (Use Type directly)

    Type t = Type.GetType("AccountingSA.Contabilidad");
    Form frmConta = (Form)Activator.CreateInstance(t);
    

    or other way using Assembly is

    Assembly assembly = typeof(Form).Assembly;
    Type t = assembly.GetType("AccountingSA.Contabilidad");
    Form frmConta = (Form)Activator.CreateInstance(t);
    

提交回复
热议问题