2011-12-22 61 views
2

我有很多类反映了我的屏幕Repository from White/UIAutomation。 要使用存储库,我需要创建许多反映我的应用程序窗口屏幕的类。如何将className传递给获取泛型类型作为参数的方法C#

要创建我使用下面的方法的存储库:

var repoReport = repository.Get<MyClassRepresentingWindow>("WindowTitle", 
InitializeOption.WithCache); 

它通过一个通用的类型,是我制备一类。

我想要做的是创建一个字典(stringClassName,字符串windowTitle)或任何地图传递给该方法。 问题是无法像Java ClassForName中那样传递className。

我试过System.Activator但没有任何成功。

Object configObj = System.Activator.CreateInstance(c); 
Type c = System.Type.GetType("Namespace.MyClassRepresentingWIndow"); 

var myClass = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("Namespace.MyClassRepresentingWIndow"); 

Type type = assembly.GetType("Namespace.MyClassRepresentingWIndow"); 
object obj = Activator.CreateInstance(type); 

var repoReport = repository.Get<c>("WindowTitle", 
InitializeOption.WithCache); 

var repoReport = repository.Get<c.Name>("WindowTitle", 
InitializeOption.WithCache); 

UPDATE1 伙计们,我不是坐在前面的代码,但我会尽量让我少的问题复杂化。

这是我在白库,我想我发现了使用方法: https://github.com/petmongrels/white/blob/itemsmap/Components/Repository/Source/ScreenRepository.cs

public virtual T Get<T>(string title, InitializeOption option) where T : AppScreen 
    { 
     ClearClosedScreens(); 
     AppScreen screen; 
     var repositoryCacheKey = new ScreenRepositoryCacheKey(title, typeof (T)); 
     if (!screenCache.TryGetValue(repositoryCacheKey, out screen)) 
     { 
      Window window = applicationSession.Application.GetWindow(title, IdentifiedOption<T>(option)); 
      screen = GetScreen<T>(window); 
      screenCache.Add(repositoryCacheKey, screen); 
     } 

     if (screen != null) 
      sessionReport.Next(typeof (T)); 
     return (T) screen; 
    } 

我记得VS显示不用彷徨的。获得<“类”型>。对不起,我不能更好地表达自己。请让我有一个病人,因为我不熟悉这个术语。

UPDATE2

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

var repoReport1 = repository.Get<MyClassRepresentingWindow1>("WindowTitle", InitializeOption.WithCache); 
var repoReport1 = repository.Get<MyClassRepresentingWindow2>("WindowTitle", InitializeOption.WithCache); 
var repoReport1 = repository.Get<MyClassRepresentingWindow3>("WindowTitle", InitializeOption.WithCache); 

而且我有MyClassRepresentingWindow{1,2,3}代码。我只是不知道如何将类名传递给Get方法。在输入中,我有这个类的字符串名称。在输出上,我想提供这种方法.Get<T>可以得到的东西。 我希望你现在能理解我。

+0

我很困惑,你正在尝试做什么。如果您可以缩小问题范围并消除噪音,则可能会更清楚您需要做什么。 – 2011-12-22 16:38:51

+2

您可以使用反射。检查此帖:http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method – Strillo 2011-12-22 16:39:24

+0

Repository.Get 如何使用泛型参数?人们会认为它也会用Activator来创建对象。 – 2011-12-22 16:58:38

回答

1

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

public string GetName<T>() 
    { 
     return typeof(T).Name; 
    } 

这导致下面的单元测试通过(以下简称“基本数学”仅仅是一个类型我曾在我的划痕应用奠定左右):

[TestMethod] 
    public void Test_Of_Generic_Type_Name() 
    { 
     var myBuilder = new GenericNamer(); 

     Assert.AreEqual<string>("BasicMath", myBuilder.GetName<BasicMath>()); 
    } 

你也可以使用该类型的全名,装配等这里是你可以使用反射拉出Type类的东西一些更多的信息:

http://msdn.microsoft.com/en-us/library/system.type.aspx

+0

嗨艾瑞克,谢谢你的回答,但我想我需要的东西相反。在输入中,我有我的类的字符串名称,我知道,我有这个类的工作代码。在输出上,我想提供一些这种方法。Get 接受。我希望你明白我的意思 – Robert 2011-12-24 09:24:58

3

为了用变量类型的参数调用它,其值只在运行时才知道,所以需要使用反射。我假设你知道如何获得表示存储库的Get<T>方法的MethodInfo

一旦你有了,这个例子说明了如何使用MethodInfo对象的基本思想。该示例假定repository类被称为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 }); 
} 

当然,您可以使其更通用,但这会使示例不太清晰。

相关问题