2

我有很多类反映了来自 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>可以获得的东西。我希望你现在能理解我。

4

2 回答 2

3

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

一旦你有了这个,这个例子说明了如何使用 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 });
}

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

于 2011-12-23T03:48:08.833 回答
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

于 2011-12-22T17:42:51.543 回答