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

发布于 2024-08-19 15:08:00 字数 483 浏览 1 评论 0原文

我正在编写一个带有参数的函数,并且该参数需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人 2024-08-26 15:08:00

取决于您的 DomainContext.Load() 函数的样子:

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

然后仍然像以前一样使用它:

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

类型系统应该推断您的意思是该函数的 LoadQuery() 版本的论点。

不幸的是,我怀疑这也意味着对上述 Load() 函数的一些修改。

Depending on what your DomainContext.Load() function looks like:

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.

Unfortunately, I suspect this will also mean some revision to the aforementioned Load() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文