[英]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.