I will get dll's dynamically. I need to load the dll and get the namespace, classname to invoke a method (the method name is static it will be always "OnStart()"). Basically I need to run a method by loading the dll. Can somebody help!!!.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
The advantage using this code is that the assembly is unloaded after use, so you can safely delete the dll after invoking the method.
To load the assembly, you would do this:
This assumes you have the assemblies on disk as files. If you don't, like if you get them from a database as a byte array, there are other methods on the Assembly that will help you give you an Assembly object after loading it.
To iterate through all the classes in the assembly, you would do this:
To find the OnStart static method, you would do this:
To call the method, you would do this:
If you need to pass arguments to the method, you would put them in an object array:
The above code can be collapsed to the following by using Linq to find the method for us:
At runtime namespaces just become part of the type name.
So you need to:
MethodInfo
for the method you want to call.Of these 2–4 are easy. 1 might be easy or might not, depending where the assembly is. Assuming the assembly can be found via the normal assembly load ("probing"). This will call a public static method of a type that takes no arguments but does have a return value.
A number of the details here will depend on the details of the assembly, type and method you want to call.
Check here: http://dotnetguts.blogspot.com/2008/12/reflection-in-c-list-of-class-name.html
And here for invoking a method: http://www.csharphelp.com/archives/archive200.html
If you search some more the terms in those links you'll find much more info.