列出方法并在运行时调用它们

时间:2012-09-12 22:48:33

标签: c# reflection singleton invoke

情况如下:

我想创建一个测试应用程序,它能够检索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);
      }
    }
  }
}

从那里开始,我真的不知道接下来要做什么来调用这些方法。 顺便说一下,这些方法可以采用一些参数并有一些返回类型。

我正在使用该接口来检索方法,以便从继承自另一个接口的方法中进行过滤。

我希望我足够清楚。对不起,我无法发布真实的代码示例,但我想这足以说明这个概念。

1 个答案:

答案 0 :(得分:1)

您应该从程序集中获取类,然后递归获取属性值并执行方法。以下是一些您应该根据自己的需求定制的简单代码:

    public void ExecuteAssembly(string anAssemblyName)
    {
        Assembly assembly = Assembly.LoadFrom(anAssemblyName);

        foreach (Type type in assembly.GetTypes())
        {
            if (type.IsClass)
            {
                TypeAttributes atts = type.Attributes;
                if ((atts & TypeAttributes.Sealed) != 0) // identify the static classes
                    ExecuteEverything(type);
            }
        }
    }

    private void ExecuteEverything(Type type)
    {
        // get only the public STATIC properties and methods declared in the type (i.e. not inherited)
        PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
        MethodInfo[] meths = type.GetMethods(BindingFlags.Public | BindingFlags.Static 
            | BindingFlags.DeclaredOnly);
        // execute the methods which aren't property accessors (identified by IsSpecialMethod = true)
        foreach (MethodInfo aMeth in meths)
            if (!aMeth.IsSpecialName)
                Execute(aMeth, type);
        // for each property get the value and recursively execute everything
        foreach (PropertyInfo aProp in props)
        {
            object aValue = aProp.GetValue(type, null);
            if (aValue != null)
                RecursivelyExecuteEverything(aValue);
        }
    }

    private void RecursivelyExecuteEverything(object aValue)
    {
        Type type = aValue.GetType();
        // get only the public INSTANCE properties and methods declared in the type (i.e. not inherited)
        PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
        MethodInfo[] meths = type.GetMethods(BindingFlags.Public | BindingFlags.Instance
            | BindingFlags.DeclaredOnly);
        // execute the methods which aren't property accessors (identified by IsSpecialMethod = true)
        foreach (MethodInfo aMeth in meths)
            if (!aMeth.IsSpecialName)
                Execute(aMeth, aValue);
        // for each property get the value and recursively execute everything
        foreach (PropertyInfo aProp in props)
        {
            object newValue = aProp.GetValue(aValue, null);
            if (newValue != null)
                RecursivelyExecuteEverything(newValue);
        }

    }

    private void Execute(MethodInfo aMeth, object anObj)
    {
        // be careful that here you should take care of the parameters. 
        // this version doesn't work for Method2 in Service2, since it 
        // requires an int as parameter
        aMeth.Invoke(anObj, null);
    }
相关问题