I have a custom attribute:
public class MenuItemAttribute : Attribute
{
}
and a class with a few methods:
public class HelloWorld
{
[MenuItemAttribute]
public void Shout()
{
}
[MenuItemAttribute]
public void Cry()
{
}
public void RunLikeHell()
{
}
}
How can I get only the methods that are decorated with the custom attribute?
So far, I have this:
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();
}
}
}
What I need now is to get the method name, the return type, as well as the parameters it accepts.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…