这是第一堂课的第一个项目:
namespace project1;
public static class Pjo
{
List<object> list1 = new List<object>();
static Pab abstate;
static Pcd cdstate;
public static Pab Abstate { get; set; }
public static Pcd Cdstate { get; set; }
public static List<object> List1 { get; set; }
public static void initialise(string elementname)
{
Pab = new Library();
List1.add(Pab);
Pcd = new Libre();
List1.add(Pcd);
}
}这是第二类的第一个项目
public class process
{
foreach(var item in Pjo.List1)
{
Type type = typeof("project1.Pjo");
Methodinfo m = type.GetMethod();// i need to invoke Library File Method. Ex:pab1()
}
}这是班级的另一个项目
namespace Lib
{
public class Library
{
int n1;
int n2;
public int N1 { get; set; }
public int N2 { get; set; }
public void Library1()
{
int n3;
n3 = n1 + n2;
}
}
public class Libre
{
int a1;
int a2;
public int A1 { get; set; }
public int A2 { get; set; }
public void Libre1()
{
int a3;
a3 = a1 + a2;
}
}
}如何使用反射或其他方法调用这个方法,在对象的c#列表中,我传递了foreach循环,我得到了类的类型,但是我不能调用这个方法。请任何人有明确的疑问。
发布于 2020-01-04 19:47:49
我可以想象这个列表包含的对象有一些共同之处(即你想要调用的方法)。
为什么不创建一个接口呢?
public interface ISomething
{
void MethodToInvoke();
}然后,不是对象列表,而是:
list<ISomething> list1 = ...然后调用接口中定义的方法:
foreach(ISomething item in new Pjo().List1)
{
item.MethodToInvoke();
}https://stackoverflow.com/questions/59590154
复制相似问题