我有很多类反映了来自 White/UIAutomation 的屏幕存储库。要使用存储库,我需要创建许多反映应用程序窗口屏幕的类。
要创建存储库,我使用以下方法:
var repoReport = repository.Get<MyClassRepresentingWindow>("WindowTitle",
InitializeOption.WithCache);
它传递了一个泛型类型,这是我准备的一个类。
我想要做的是创建一个 Dictionary(stringClassName, string windowTitle) 或任何 Map 传递给该方法。问题是不能像 Java ClassForName 那样传递类名。
我试过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 伙计们,我不是坐在代码前面,但我会尽量让我的问题不那么复杂。
这是我在我认为我使用的 White 存储库中找到的一种方法: 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 将 .Get 显示为 .Get<"class" type>。对不起,我不能更好地表达自己。请有一个病人陪我,因为我不熟悉这个术语。
更新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);
我有一个代码MyClassRepresentingWindow{1,2,3}
。我只是不知道如何将类名传递给 Get 方法。在输入时,我有一个此类的字符串名称。在输出时,我想提供这种方法.Get<T>
可以获得的东西。我希望你现在能理解我。