相关疑难解决方法(0)

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

您好我正在尝试使用C#反射来调用传递参数的方法,并作为回报传递回结果.我怎样才能做到这一点?我尝试了几件事但没有成功.我已经习惯了PHP和Python,这可以在一行上完成,所以这对我来说非常困惑.

实质上,这就是如何在没有反射的情况下进行调用:

response = service.CreateAmbience(request);
Run Code Online (Sandbox Code Playgroud)

请求有以下对象:

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"]);
Run Code Online (Sandbox Code Playgroud)

这是我实现反射的函数,其中上面案例的serviceAction将是"CreateAmbience":

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

c# reflection

19
推荐指数
2
解决办法
4万
查看次数

如何将Class作为方法的参数传递?

我有两个班:

Class Gold;
Class Functions;
Run Code Online (Sandbox Code Playgroud)

有一种方法ClassGet在类Functions,其具有2个参数.我想将类Gold作为参数发送给我在课堂上的一个方法Functions.这怎么可能?

例如:

public void ClassGet(class MyClassName, string blabla)
{
    MyClassName NewInstance = new MyClassName();
}
Run Code Online (Sandbox Code Playgroud)

注意:我想将MyClassName字符串参数发送到我的方法.

c# methods class function parameter-passing

6
推荐指数
3
解决办法
5万
查看次数

标签 统计

c# ×2

class ×1

function ×1

methods ×1

parameter-passing ×1

reflection ×1