Well, I'm trying to invoke some methods of my object using Reflection, but even though every place that I saw how to do that it is made like that, when I try it, it doesn't work. My code look like that:
using System;
using System.Collections.Generic;
using System.Reflection;
public class Program
{
static void Main()
{
List <Person> myList = new List<Person>();
myList.Add(new Person());
myList.Add(new Person());
InvokeMethods(myList);
}
static void InvokeMethods(List<Person> myList)
{
foreach(Person person in myList)
{
MethodInfo[] methods = person.GetType().GetMethods();
foreach(MethodInfo method in methods)
{
method.Invoke(person, null);
}
}
}
}
public class Person
{
private string name;
public string Name { get => name; set => name = value; }
public Person()
{
name = "Default";
}
public void SetTalking()
{
Console.WriteLine("{0} talking...", name);
}
public void SetWalking()
{
Console.WriteLine("{0} walking...", name);
}
}
But, when I run this, I'm getting:
System.Reflection.TargetParameterCountException: Parameter count mismatch.
So, isn't it supposed to work since my methods don't have any arguments?
Aucun commentaire:
Enregistrer un commentaire