我有一个简单的挑战.我动态地需要在C#中找出具有特定属性的所有方法.我将从另一个应用程序动态加载程序集,需要找出确切的方法.程序集如下所示:
Base.dll:
Class Base
{
[testmethod]
public void method1()
...
}
Run Code Online (Sandbox Code Playgroud)
Derived.dll:
Class Derived:Base
{
[testmethod]
public void method2()
}
Run Code Online (Sandbox Code Playgroud)
现在在第3个应用程序中,我动态地想加载上面提到的dll并找出testmethods.
如果我加载Base.dll,我需要获得testmethod1.如果我加载Drived.dll,我应该得到testmethod1和testmethod2.
我在网上找到了一些帮助我动态加载dll的代码:
List<Assembly> a = new List<Assembly>();
string bin = @"Bin-Folder";
DirectoryInfo oDirectoryInfo = new DirectoryInfo(bin);
//Check the directory exists
if (oDirectoryInfo.Exists)
{
//Foreach Assembly with dll as the extension
foreach (FileInfo oFileInfo in oDirectoryInfo.GetFiles("*.dll", SearchOption.AllDirectories))
{
Assembly tempAssembly = null;
//Before loading the assembly, check all current loaded assemblies in case talready loaded
//has already been loaded as …Run Code Online (Sandbox Code Playgroud)