在接口中包括一个通用类型参数,由接口约束

发布于 2025-01-30 18:16:00 字数 1105 浏览 3 评论 0原文

我被限制在接口约束的实现上。我的用法对我是直觉的,但没有编译,所以我误解了一些东西。

我的接口:

interface IEntity
{
    int ExampleMethod(IContext<IFooBar> context);
}

interface IContext<T> where T : class, IFooBar
{
    T FooBar { get; set; }
}

interface IFooBar
{
    int Value { get; }
}

我的实现:

class Entity : IEntity
{
    public int ExampleMethod(IContext<IFooBar> context)
    {
        return context.FooBar.Value;
    }
}

class Context : IContext<FooBar>
{
    public FooBar FooBar { get; set; }
}

class FooBar : IFooBar
{
    public int Value { get { return 10; } }
}

实体类的用法,在其中抛出了

class UsageOfEntity
{
    public UsageOfEntity()
    {
        var context = new Context();
        var entity = new Entity();

        int result = entity.ExampleMethod(context);
    }
}

实例上下文引发错误:

参数1:无法从“上下文”转换为'iContext&lt; ifoobar&gt;'

如何约束通用类型参数以便可以使用我的实现?

I am stuck on the usage of an implementation that is constraint by an interface. My usage is intuitive to me, but does not compile so I am misunderstanding something.

My interfaces:

interface IEntity
{
    int ExampleMethod(IContext<IFooBar> context);
}

interface IContext<T> where T : class, IFooBar
{
    T FooBar { get; set; }
}

interface IFooBar
{
    int Value { get; }
}

My implementations:

class Entity : IEntity
{
    public int ExampleMethod(IContext<IFooBar> context)
    {
        return context.FooBar.Value;
    }
}

class Context : IContext<FooBar>
{
    public FooBar FooBar { get; set; }
}

class FooBar : IFooBar
{
    public int Value { get { return 10; } }
}

Usage of Entity class, where problem is thrown

class UsageOfEntity
{
    public UsageOfEntity()
    {
        var context = new Context();
        var entity = new Entity();

        int result = entity.ExampleMethod(context);
    }
}

The usage of instance context throws an error:

Argument 1: cannot convert from 'Context' to 'IContext<IFooBar>'

How do I constrain the generic type parameter such that my implementation can be used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦里的微风 2025-02-06 18:16:00

上下文icontext&lt; foobar&gt;不是icontext&lt; ifoobar&gt;

因为op在注释中指出icontext&lt; t&gt; .foobar仅需要仅阅读,所以t可以使得协变量:

interface IContext<out T>
where T : class, IFooBar
{
    T FooBar { get; }
}

现在,因为foobar实现ifoobar,使用icontext&lt; foobar&gt;代替icontext&lt; ifoobar&gt;::

Context is an IContext<FooBar> not an IContext<IFooBar>.

Because the OP has indicated in the comments that IContext<T>.FooBar only needs to be read-only, T can be made covariant:

interface IContext<out T>
where T : class, IFooBar
{
    T FooBar { get; }
}

Now, because FooBar implements IFoobar, it is valid to use a IContext<FooBar> in place of a IContext<IFooBar>:

浅沫记忆 2025-02-06 18:16:00

代码中的问题是,您正在尝试将上下文转换为type icontext 就像您的错误告诉您一样。

{
    public UsageOfEntity()
    {
        var context = new Context();
        var entity = new Entity();

        int result = entity.ExampleMethod(context);
    }
}

您正在尝试将上下文传递给icontext在此行中,

entity.ExampleMethod(context);

您应该在type中传递 exipplemethod() < 代码>上下文&lt; foobar&gt; 。
我还想指出,为poco类制作界面是不必要的,只需保持正常的类即可。

您的代码应该看起来像这样:

class Entity : IEntity
{
    public int ExampleMethod(Context<FooBar> context)
    {
        return context.FooBar.Value;
    }
}

interface IEntity
{
    int ExampleMethod(IContext<IFooBar> context);
}

The problem in your code is that you are trying to convert Context to Type IContext just as your error tells you.

{
    public UsageOfEntity()
    {
        var context = new Context();
        var entity = new Entity();

        int result = entity.ExampleMethod(context);
    }
}

You are trying to pass Context to IContext in this line

entity.ExampleMethod(context);

You should make the Type passed in ExampleMethod() Context<FooBar>.
I would also like to point out that making an interface for a POCO class is unnecessary, just keep it a normal class.

Your code should look like this:

class Entity : IEntity
{
    public int ExampleMethod(Context<FooBar> context)
    {
        return context.FooBar.Value;
    }
}

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