简体   繁体   English

如何在C#中的运行时设置TSomething类型?

[英]How do I set a Type TSomething at run time in C#?

I am writting a function that takes a parameter and that parameter requires a Type of TEntity. 我正在编写一个带有参数的函数,该参数需要TEntity的类型。 I want to be able to pass it a specific Type during runtime but I am having trouble getting it to compile: 我希望能够在运行时将其传递给特定类型,但是我无法使其进行编译:

public LoadOperation LoadQuery(EntityQuery<???> query)
        {
            LoadOperation loadOperation = DomainContext.Load(query,LoadBehavior.MergeIntoCurrent, false);
            return loadOperation;
        }

The code that wont compile looks like this: 无法编译的代码如下所示:

EntityQuery<Person> q = DomainContext.GetPerson();
LoadQuery(q);

I have tried different things to make this work but am at a loss. 我尝试了不同的方法使这项工作奏效,但茫然。 What do I need to do? 我需要做什么?

Depending on what your DomainContext.Load() function looks like: 根据您的DomainContext.Load()函数的外观:

public LoadOperation LoadQuery<T>(EntityQuery<T> query)
{
    LoadOperation loadOperation = DomainContext.Load(query,LoadBehavior.MergeIntoCurrent, false);
    return loadOperation;
}

And then still use it exactly like you did before: 然后仍然像以前一样使用它:

EntityQuery<Person> q = DomainContext.GetPerson();
LoadQuery(q);

The type system should infer you mean the LoadQuery<Person>() version of the function from the argument. 类型系统应该从参数推断您的意思是LoadQuery<Person>()版本。

Unfortunately, I suspect this will also mean some revision to the aforementioned Load() function. 不幸的是,我怀疑这还将意味着对上述Load()函数进行一些修订。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何同时运行控制台和 Forms? c# - How do I run Console and Forms at the same time? c# 如何在C#中推断类的类型,然后在运行时分配值? - How to infer the type of class and then assign values at run time in C#? 如何在c#,winforms的运行时编译中传递参数? - How do I pass parameters in run-time compilation in c#, winforms? 如何在表达式树 C# 中使用运行时生成的类型 - How to use run time generated type in expression tree C# 如何在运行时设置过滤器? 用于C#,Winforms,DevExpress中的gridview - How to set Filter in run time ? for gridview in C#, Winforms, DevExpress 如何从C#类方法中找到调用哪些方法 - 而不是在运行时 - How do I find what methods are called from a C# class method - NOT at run time 我如何在特定时间段内运行 do while c# - How can i run do while for specific time period c# 如何同时在多个浏览器上运行测试? 硒网格,C#,Specflow,NUnit - How do I run a test on multiple browsers at the same time? Selenium Grid, C#, Specflow, NUnit C#-如何在运行时获取“带有通用参数的类”的Type? - C# - How do I GetType of 'class with generic parameter' in run-time? 我如何在运行时在C#中操作字体属性 - How can i manipulate the Font Properties in c# on a run time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM