如何使用反射的名字来调用方法 [英] How to use reflection to call method by name

查看:160
本文介绍了如何使用反射的名字来调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想使用C#反射来调用传递参数的方法,作为回报,传回的结果。我该怎么办呢?我试了几件事情,但没有成功。我已经习惯了PHP和Python这样的地方可以在一行来完成,所以这是非常混乱给我。

在本质上,这是怎样的调用将没有反射进行:

  =响应service.CreateAmbience(请求);

请求有以下对象:

  request.UserId =(长)Constants.defaultAmbience [用户ID];
request.Ambience.CountryId =(长[])Constants.defaultAmbience [CountryId];
request.Ambience.Name.DefaultText =(字符串)Constants.defaultAmbience [NameDefaultText];
request.Ambience.Name.LanguageText = GetCultureTextLanguageText((串)Constants.defaultAmbience [NameCulture],(串)Constants.defaultAmbience [NameText]);
request.Ambience.Description.DefaultText =(字符串)Constants.defaultAmbience [descriptiontext描述];
request.Ambience.Description.LanguageText = GetCultureTextLanguageText((串)Constants.defaultAmbience [DescriptionCulture],(串)Constants.defaultAmbience [DescriptionDefaultText]);

这是我的功能来实现的反射,其中serviceAction对于上述情况将是CreateAmbience

 公共静态řResponseHelper< T,R>(T请求字符串serviceAction)
{
    ICMSCoreContentService服务=新ContentServiceRef.CMSCoreContentServiceClient();
     - [R =响应默认值(R);
    响应=?
}


解决方案

线沿线的东西:

  MethodInfo的方法= service.GetType()实现getMethod(serviceAction)。
对象result = method.Invoke(服务,新的对象[] {要求});
收益率(R)的结果;

您可能想在每个级别虽然加检查,以确保有问题的方法实际上是有效的,它有正确的参数类型,并且它有正确的返回类型。这应该足以让你开始虽然。

Hi I am trying to use C# reflection to call a method that is passed a parameter and in return passes back a result. 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.

In essence this is how the call would be made without reflection:

response = service.CreateAmbience(request);

request has these objects:

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":

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.

这篇关于如何使用反射的名字来调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆