How to do dynamic object creation and method invocation in .NET 3.5

后端 未结 4 951
广开言路
广开言路 2021-01-06 23:52

How does the code looks that would create an object of class:

string myClass = \"MyClass\";

Of the above type, and then call



        
4条回答
  •  广开言路
    2021-01-07 00:24

    The following assumes an object with a public constructor and a public method that returns some value but takes no parameters.

    var object = Activator.CreateInstance( "MyClass" );
    var result = object.GetType().GetMethod( "MyMethod" ).Invoke( object, null );
    

提交回复
热议问题