简体   繁体   English

如何使用反射按名称调用方法

[英]How to use reflection to call method by name

Hi I am trying to use C# reflection to call a method that is passed a parameter and in return passes back a result.嗨,我正在尝试使用 C# 反射来调用传递参数并返回结果的方法。 How can I do that?我怎样才能做到这一点? I've tried a couple of things but with no success.我尝试了几件事,但没有成功。 I'm used to PHP and Python where this can be done on a single line so this is very confusing to me.我习惯了 PHP 和 Python,它们可以在一行上完成,所以这对我来说很困惑。

In essence this is how the call would be made without reflection:本质上,这就是在没有反射的情况下进行调用的方式:

response = service.CreateAmbience(request);

request has these objects: request 有这些对象:

request.UserId = (long)Constants.defaultAmbience["UserId"];
request.Ambience.CountryId = (long[])Constants.defaultAmbience["CountryId"];
request.Ambience.Name.DefaultText = (string)Constants.defaultAmbience["NameDefaultText"];
request.Ambience.Name.LanguageText = GetCultureTextLanguageText((string)Constants.defaultAmbience["NameCulture"], (string)Constants.defaultAmbience["NameText"]);
request.Ambience.Description.DefaultText = (string)Constants.defaultAmbience["DescriptionText"];
request.Ambience.Description.LanguageText = GetCultureTextLanguageText((string)Constants.defaultAmbience["DescriptionCulture"], (string)Constants.defaultAmbience["DescriptionDefaultText"]);

This is my function to implement the reflection where serviceAction for the case above would be "CreateAmbience":这是我实现反射的函数,其中上面案例的 serviceAction 将是“CreateAmbience”:

public static R ResponseHelper<T,R>(T request, String serviceAction)
{
    ICMSCoreContentService service = new ContentServiceRef.CMSCoreContentServiceClient();
    R response = default(R);
    response = ???
}

Something along the lines of:类似的东西:

MethodInfo method = service.GetType().GetMethod(serviceAction);
object result = method.Invoke(service, new object[] { request });
return (R) result;

You may well want to add checks at each level though, to make sure the method in question is actually valid, that it has the right parameter types, and that it's got the right return type.不过,您可能希望在每个级别添加检查,以确保所讨论的方法实际上有效,它具有正确的参数类型,并且它具有正确的返回类型。 This should be enough to get you started though.不过,这应该足以让您入门。

Here's a quick example of calling an object method by name using reflection:这是使用反射按名称调用对象方法的快速示例:

Type thisType = <your object>.GetType();
MethodInfo theMethod = thisType.GetMethod(<The Method Name>); 
theMethod.Invoke(this, <an object [] of parameters or null>); 

If you're on .NET 4, use dynamic :如果您使用 .NET 4,请使用dynamic

dynamic dService = service;
var response = dService.CreateAmbience(request);

You can use Delegate.CreateDelegate to obtain a delegate to the method by name:您可以使用Delegate.CreateDelegate通过名称获取该方法的委托:

public static R ResponseHelper<T,R>(T request, string serviceAction)
{
    var service = new ContentServiceRef.CMSCoreContentServiceClient();

    var func = (Func<T,R>)Delegate.CreateDelegate(typeof(Func<T,R>),
                                                  service,
                                                  serviceAction);

    return func(request);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM