我有一个自定义属性:
public class MenuItemAttribute : Attribute
{
}
和一个有几个方法的类:
public class HelloWorld
{
[MenuItemAttribute]
public void Shout()
{
}
[MenuItemAttribute]
public void Cry()
{
}
public void RunLikeHell()
{
}
}
如何仅获取使用自定义属性修饰的方法?
到目前为止,我有这个:
string assemblyName = fileInfo.FullName;
byte[] assemblyBytes = File.ReadAllBytes(assemblyName);
Assembly assembly = Assembly.Load(assemblyBytes);
foreach (Type type in assembly.GetTypes())
{
System.Attribute[] attributes = System.Attribute.GetCustomAttributes(type);
foreach (Attribute attribute in attributes)
{
if (attribute is MenuItemAttribute)
{
//Get me the method info
//MethodInfo[] methods = attribute.GetType().GetMethods();
}
}
}
我现在需要的是获取方法名称、返回类型以及它接受的参数。