如何在 C# 中运行时设置类型 TSomething?
我正在编写一个带有参数的函数,并且该参数需要 TEntity 类型。我希望能够在运行时向它传递一个特定的类型,但我在编译它时遇到了麻烦:
public LoadOperation LoadQuery(EntityQuery<???> query)
{
LoadOperation loadOperation = DomainContext.Load(query,LoadBehavior.MergeIntoCurrent, false);
return loadOperation;
}
无法编译的代码如下所示:
EntityQuery<Person> q = DomainContext.GetPerson();
LoadQuery(q);
我尝试了不同的方法来使这项工作正常进行,但我不知所措。我需要做什么?
I am writting a function that takes a parameter and that parameter requires a Type of 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
取决于您的 DomainContext.Load() 函数的样子:
然后仍然像以前一样使用它:
类型系统应该推断您的意思是该函数的
LoadQuery()
版本的论点。不幸的是,我怀疑这也意味着对上述 Load() 函数的一些修改。
Depending on what your DomainContext.Load() function looks like:
And then still use it exactly like you did before:
The type system should infer you mean the
LoadQuery<Person>()
version of the function from the argument.Unfortunately, I suspect this will also mean some revision to the aforementioned Load() function.