情况如下:
我想创建一个测试应用程序,它能够检索 dll 中的所有类和方法,并允许我在运行时调用它们。
我所拥有的是这样的:
假设我有这些课程:
public static class FirstManagerSingleton
{
    public static SecondManager Instance
    {
       return mInstance; // which is a SecondManager private static object
    }
}
public class SecondManager
{
    public Service1 service1 {get; private set;}
    public Service2 service2 {get; private set;}
    ...
}
public class Service1
{
    public bool Method1()
    {
      return true;
    }
    public int Method2()
    {
      return 1;
    }
    ...
}
public class Service2
{
    public bool Method1()
    {
      return false;
    }
    public int Method2(int aNumber)
    {
      return aNumber - 1;
    }
    ...
}
我希望能够选择每个“服务”类,调用任何方法并显示其结果。
是否可以使用反射来做到这一点?如果它不是多层的(2 个经理类),我不会那么挣扎。事实是我需要通过如下所示的调用访问服务类:
FirstManagerSingleton.Instance.Service1.Method1;
到目前为止,我已经能够加载程序集并检索几乎所有方法并打印它们。
Assembly assembly = Assembly.LoadFrom("assemblyName");
// through each type in the assembly
foreach (Type type in assembly.GetTypes())
{
  // Pick up a class
  if (type.IsClass == true)
  {
    MethodInfo[] methodInfo;
    Console.WriteLine("Found Class : {0}", type.FullName);
    Type inter = type.GetInterface("I" + type.Name, true);
    if (inter != null)
    {
      methodInfo = inter.GetMethods();
      foreach (MethodInfo aMethod in test2)
      {
        Console.WriteLine("\t\tMethods : " + aMethod);
      }
    }
  }
}
从那里开始,我真的不知道接下来要做什么来调用这些方法。顺便说一句,这些方法可以接受一些参数并有一些返回类型。
我正在使用该接口来检索方法,以便从从另一个接口继承的方法中进行过滤。
我希望我足够清楚。抱歉,我无法发布真正的代码示例,但我想这足以说明这个概念。