C# 如何将类名传递给在C中获取泛型类型作为参数的方法#

C# 如何将类名传递给在C中获取泛型类型作为参数的方法#,c#,class,generics,white-framework,C#,Class,Generics,White Framework,我有很多类反映了White/UIAutomation中的屏幕存储库。 要使用存储库,我需要创建大量反映应用程序窗口屏幕的类 要创建存储库,我使用以下方法: var repoReport = repository.Get<MyClassRepresentingWindow>("WindowTitle", InitializeOption.WithCache); 更新1 伙计们,我不是坐在代码前面,但我会尽量让我的问题不那么复杂 这是我在White存储库中找到的一种方法,我认为我使用

我有很多类反映了White/UIAutomation中的屏幕存储库。 要使用存储库,我需要创建大量反映应用程序窗口屏幕的类

要创建存储库,我使用以下方法:

var repoReport = repository.Get<MyClassRepresentingWindow>("WindowTitle", 
InitializeOption.WithCache);
更新1 伙计们,我不是坐在代码前面,但我会尽量让我的问题不那么复杂

这是我在White存储库中找到的一种方法,我认为我使用了:

public virtual T Get(字符串标题,初始化选项),其中T:AppScreen
{
ClearClosedScreens();
应用屏幕;
var repositoryCacheKey=新屏幕repositoryCacheKey(标题,类型(T));
如果(!screenCache.TryGetValue(repositoryCacheKey,屏幕外))
{
Window Window=applicationSession.Application.GetWindow(标题,标识选项(选项));
screen=GetScreen(窗口);
添加(repositoryCacheKey,screen);
}
如果(屏幕!=null)
会议报告。下一个(类型(T));
返回(T)屏幕;
}
正如我所记得的那样,VS显示了.Get作为一个.Get。对不起,我无法更好地表达自己。请找个病人陪我,因为我不熟悉这个术语

更新2

最后,我想得到这样的东西:

var repoReport1 = repository.Get<MyClassRepresentingWindow1>("WindowTitle", InitializeOption.WithCache);
var repoReport1 = repository.Get<MyClassRepresentingWindow2>("WindowTitle", InitializeOption.WithCache);
var repoReport1 = repository.Get<MyClassRepresentingWindow3>("WindowTitle", InitializeOption.WithCache);
    public string GetName<T>()
    {
        return typeof(T).Name;
    }
var repoReport1=repository.Get(“WindowTitle”,InitializeOption.WithCache);
var repoReport1=repository.Get(“WindowTitle”,InitializeOption.WithCache);
var repoReport1=repository.Get(“WindowTitle”,InitializeOption.WithCache);
我有一个代码
MyClassRepresentingWindow{1,2,3}
。我只是不知道如何传递类名来获取方法。输入时,我有一个此类的字符串名称。在输出时,我希望传递此方法
.Get
可以获得的内容。
我希望你现在能理解我。

我相信你想要的是这样的东西:

var repoReport1 = repository.Get<MyClassRepresentingWindow1>("WindowTitle", InitializeOption.WithCache);
var repoReport1 = repository.Get<MyClassRepresentingWindow2>("WindowTitle", InitializeOption.WithCache);
var repoReport1 = repository.Get<MyClassRepresentingWindow3>("WindowTitle", InitializeOption.WithCache);
    public string GetName<T>()
    {
        return typeof(T).Name;
    }
公共字符串GetName()
{
返回typeof(T).Name;
}
这导致以下单元测试通过(“基本数学”只是我在scratch应用程序中使用的一种类型):

[TestMethod]
公共无效测试(通用类型名称)()
{
var myBuilder=new GenericNamer();
AreEqual(“BasicMath”,myBuilder.GetName());
}
您还可以使用类型的全名、程序集等。以下是有关使用反射从类型类中提取内容的更多信息:


要使用变量类型参数调用此函数(其值仅在运行时已知),需要使用反射。我假设您知道如何获取表示存储库的
get
方法的
MethodInfo

一旦你有了这些,这个例子说明了如何使用MethodInfo对象的基本思想。该示例假定
存储库
类被称为
Repo
;根据需要进行更改:

object InvokeGenericMethod(Repo repository, MethodInfo method, Type typeArg, string arg1, InitializeOption arg2)
{
    MethodInfo constructedMethod = method.MakeGenericMethod(typeArg);
    return constructedMethod.Invoke(repository, new object[] { arg1, arg2 });
}

当然,您可以让这个更通用,但这会使示例不那么清晰。

我不知道您想做什么。如果你能缩小你的问题范围,消除噪音,你需要做的事情可能会更清楚。你可以使用反射。查看此帖子:存储库如何。是否使用泛型参数?我更新了文本,添加了一个我认为正在使用的方法代码。伙计们,我现在无法查看你们的建议/解决方案。圣诞节刚过,我就挑战性地这么做。圣诞快乐,新年快乐!嗨,埃里克,谢谢你的回答,但我想我需要一些相反的东西。在输入时,我有一个我知道的类的字符串名,并且我有一个这个类的工作代码。在输出时,我希望传递此方法接受的内容。我希望你明白我的意思
object InvokeGenericMethod(Repo repository, MethodInfo method, Type typeArg, string arg1, InitializeOption arg2)
{
    MethodInfo constructedMethod = method.MakeGenericMethod(typeArg);
    return constructedMethod.Invoke(repository, new object[] { arg1, arg2 });
}