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

后端 未结 4 1016
广开言路
广开言路 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:06

    I've created a library which simplifies dynamic object creation and invocation using .NET you can download the library and the code in google code: Late Binding Helper In the project you will find a Wiki page with the usage, or you can also check this article in CodeProject

    Using my library, your example will look like this:

    IOperationInvoker myClass = BindingFactory.CreateObjectBinding("MyClassAssembly", "MyClass");
    myClass.Method("MyMethod").Invoke();
    

    Or even shorter:

    BindingFactory.CreateObjectBinding("MyClassAssembly", "MyClass")
         .Method("MyMethod")
         .Invoke();
    

    It uses a fluent interface, and truly simplifies this kind of operations. I hope you could find it useful.

提交回复
热议问题