如何反映存储在对象中的方法

时间:2014-10-13 14:26:30

标签: c# reflection

我的代码是:

private Dictionary<string, object> services;
public void AddService<T>( string serviceName, T func )
{
    if ( services.ContainsKey( serviceName ) )
    {
        services.Add( serviceName, func );
    }
}


public void A()
{
    /* doing */
}


public void B( string a )
{
    /* doing */
}


public string C( string a )
{
    /* doing */
    return(a);
}


[TestMethod]
public void Test()
{
    services = new Dictionary<string, object>();

    AddService<Action>( "Aservice", A );
    AddService<Action<string> >( "Bservice", B );
    AddService<Func<string, string>>( "Cservice", C );


    foreach ( var service in services )
    {
        /*
         * How to print these service like this:
         * void A()
         * void B(string xxx)
         * string C(string xxx)
         */
    }
}

我想将一些函数作为服务引入Dictionary,并在需要时调用它们。 因为我无法确定添加的服务的类型,所以我使用T来替换Action<string>Func<string>或其他人。

我可以使用这些代码来调用Aservice,只需要将object转换为Aservice的类型

var a = services["Aservice"] as Action;
a();

但是当第三方要求任何服务时,他必须知道服务的类型。

所以,我想打印这些服务的类型,包括argumentType,returnType,functionName。

我尝试使用反射,但没有好结果。

我无法使用System.Reflection.RuntimeMethodInfo,因为无法访问内部。

你能帮助我吗?

0 个答案:

没有答案
相关问题