2011-12-22 59 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 }); 
} 

當然,您可以使其更通用,但這會使示例不太清晰。

相關問題