我有一个简单的挑战。我动态地需要找出 C# 中具有特定属性的所有方法。我要从另一个应用程序动态加载程序集,需要找出确切的方法。程序集如下所示:
基础.dll:
Class Base
{
[testmethod]
public void method1()
...
}
派生.dll:
Class Derived:Base
{
[testmethod]
public void method2()
}
现在在第 3 个应用程序中,我喜欢动态加载上述 dll 并找出测试方法。
如果我加载 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 a reference to another assembly
//Loading the assembly twice can cause major issues
foreach (Assembly loadedAssembly in AppDomain.CurrentDomain.GetAssemblies())
{
//Check the assembly is not dynamically generated as we are not interested in these
if (loadedAssembly.ManifestModule.GetType().Namespace != "System.Reflection.Emit")
{
//Get the loaded assembly filename
string sLoadedFilename =
loadedAssembly.CodeBase.Substring(loadedAssembly.CodeBase.LastIndexOf('/') + 1);
//If the filenames match, set the assembly to the one that is already loaded
if (sLoadedFilename.ToUpper() == oFileInfo.Name.ToUpper())
{
tempAssembly = loadedAssembly;
break;
}
}
}
//If the assembly is not aleady loaded, load it manually
if (tempAssembly == null)
{
tempAssembly = Assembly.LoadFrom(oFileInfo.FullName);
}
a.Add(tempAssembly);
}
上面的代码工作正常,我可以加载 DLL。但是,当我使用以下代码找出正确的方法时,它不会返回任何所需的结果。我想知道哪一部分不正确。以下代码列出了大约 145 个方法,但没有一个是我正在寻找的方法。
public static List<string> GetTests(Type testClass)
{
MethodInfo[] methodInfos = testClass.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
Array.Sort(methodInfos,
delegate(MethodInfo methodInfo1, MethodInfo methodInfo2)
{ return methodInfo1.Name.CompareTo(methodInfo2.Name); });
foreach (MethodInfo mi in methodInfos)
{
foreach (var item in mi.GetCustomAttributes(false))
{
if
(item.ToString().CompareTo("Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute") == 0)
result.Add(mi.Name);
}
}
return result;
}
谁能帮我解决这个问题?
我不确定为什么,但我尝试从上述类(Base 和 Derived)中实例化对象,并且上述代码返回了正确的结果。但是,如上所述,如果我没有来自基类和派生类的对象并尝试根据类型找出方法,它不会返回所需的结果。
谢谢
最佳答案
最简单的方法是使用 MethodInfo.IsDefined
- 很可能还有 LINQ:
var testMethods = from assembly in assemblies
from type in assembly.GetTypes()
from method in type.GetMethods()
where method.IsDefined(typeof(TestMethodAttribute))
select method;
foreach (var method in testMethods)
{
Console.WriteLine(method);
}
(我也会使用 LINQ 进行所有排序。例如,显然您可以将 GetMethods
调用等调整为仅返回实例方法。)
我不完全清楚为什么您当前的方法不起作用或者为什么它在您创建实例后确实起作用 - 但如果没有一个简短但完整的示例来说明问题,它将是很难进一步诊断。我肯定会从上面的代码开始:)
关于c# - 如何动态找出所有具有自定义属性的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18905236/