I'm trying to understand how can I use custom attributes to call method passing a parameters
[ExecuteMe("hello", "reflection")]
public void M3(string s1, string s2)
{
Console.WriteLine("M3 s1={0} s2={1}", s1, s2);
}
I'm trying to call this method using this code:
static void Main(string[] args)
{
var assembly= Assembly.LoadFrom("MyLibrary.dll");
foreach (var type in assembly.GetTypes())
{
object act = Activator.CreateInstance(type);
var methodInfos = type.GetMethods().Where(m => m.GetCustomAttributes(typeof(ExecuteMe)).Any());
foreach (var mInfo in methodInfos)
{
//Console.WriteLine(mInfo.Name);
var argument = mInfo.GetParameters();
foreach (var a in argument)
{
Console.WriteLine(a);
// a.RawDefaultValue;
mInfo.Invoke(act, new object[]{a});
}
}
if (type.IsClass)
Console.WriteLine(type.FullName);
}
Console.ReadLine();
}
It doesn't work because "a" is a ParameterInfo and invoke want a Object[]. What am I doing wrong and how do I get those values?
this is my attribute:
public class ExecuteMe : Attribute
{
public object[] args;
public ExecuteMe(params object[] _args)
{
this.args = _args;
}
}`
Aucun commentaire:
Enregistrer un commentaire