使用通用隐式运算符有什么问题?

发布于 2024-12-27 03:02:08 字数 1642 浏览 1 评论 0原文

如果我在非泛型构建器类中使用隐式运算符,则一切正常:

 public class ReligionBuilder
{
    private Religion _religion;

    public ReligionBuilder()
    {
        _religion = new Religion(){//some codes}

    }

    public ReligionBuilder AddToRepository()
    {
        Repository<Religion>.Add(_religion);
        return this;
    }
    public Religion Build()
    {
        return _religion;
    }

    public static implicit operator Religion(ReligionBuilder _builder)
    {
        return _builder.Build();

    }
}

我可以使用它:

 Religion religion=new ReligionBuilder().AddToRepository();

但如果此运算符在泛型类中,则出现问题:

 public abstract class DataTestBuilderBase<T> : IDataTestBuilder<T>
{
    protected T TestData { get; set; }

    public virtual T Build()
    {
        return TestData;
    }

    public abstract IDataTestBuilder<T> AddToRepository();
    public abstract IDataTestBuilder<T> WithDefault();

    public static implicit operator T(DataTestBuilderBase<T> builder)
    {
        return builder.Build();
    }
}


 public class PersonDataTestBuilder : DataTestBuilderBase<Person>
{
    private Person _person;

    public PersonDataTestBuilder()
    {
        //some codes
    }
    public override IDataTestBuilder<Person> AddToRepository()
    {
       //some codes
        return this;
    }
}

用法:

 PersonDataTestBuilder _testBuilder = new PersonDataTestBuilder();
        Person person = _testBuilder.AddToRepository();

错误是:无法将 IDataTestBuilder 转换为 Person

问题是什么?

if I use implicit operator in non generic builder class every thing is ok:

 public class ReligionBuilder
{
    private Religion _religion;

    public ReligionBuilder()
    {
        _religion = new Religion(){//some codes}

    }

    public ReligionBuilder AddToRepository()
    {
        Repository<Religion>.Add(_religion);
        return this;
    }
    public Religion Build()
    {
        return _religion;
    }

    public static implicit operator Religion(ReligionBuilder _builder)
    {
        return _builder.Build();

    }
}

I can use it :

 Religion religion=new ReligionBuilder().AddToRepository();

but if this operator is in generic class something is wrong:

 public abstract class DataTestBuilderBase<T> : IDataTestBuilder<T>
{
    protected T TestData { get; set; }

    public virtual T Build()
    {
        return TestData;
    }

    public abstract IDataTestBuilder<T> AddToRepository();
    public abstract IDataTestBuilder<T> WithDefault();

    public static implicit operator T(DataTestBuilderBase<T> builder)
    {
        return builder.Build();
    }
}


 public class PersonDataTestBuilder : DataTestBuilderBase<Person>
{
    private Person _person;

    public PersonDataTestBuilder()
    {
        //some codes
    }
    public override IDataTestBuilder<Person> AddToRepository()
    {
       //some codes
        return this;
    }
}

usage:

 PersonDataTestBuilder _testBuilder = new PersonDataTestBuilder();
        Person person = _testBuilder.AddToRepository();

the error is :cannot convert IDataTestBuilder to Person

what is the problem?

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

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

发布评论

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

评论(1

简单气质女生网名 2025-01-03 03:02:08

AddToRepository 仅返回 IDataTestBuilder(就编译时返回类型而言) - 并且没有从 that的隐式转换>人。如果将抽象方法的返回类型更改为 DataTestBuilderBase那么它应该可以工作 - 尽管坦率地说我无论如何都不想使用该隐式转换。我通常对提供隐式转换非常谨慎 - 它们通常会使代码不太清晰,就像我相信它们在这里所做的那样。

您真的应该在构建器上提供AddToRepository吗?对于构建器来说,这感觉像是一个不适当的操作 - 我希望:

Person person = new PersonBuilder { /* properties */ }
                     .Build()
                     .AddToRepository();

编辑:只是为了展示我关于更改 AddToRepository 返回类型的含义,这里有一个简短但完整的程序来演示。效果很好。

using System;

public abstract class BuilderBase<T>
{
    public abstract T Build();
    public abstract BuilderBase<T> AddToRepository();

    public static implicit operator T(BuilderBase<T> builder)
    {
        return builder.Build();
    }
}

public class TestBuilder : BuilderBase<string>
{
    public override string Build()
    {
        return "Built by Build()";
    }

    public override BuilderBase<string> AddToRepository()
    {
        return this;
    }
}

class Program
{
    static void Main(string[] args)
    {
        string x = new TestBuilder().AddToRepository();
        Console.WriteLine(x);
    }
}

AddToRepository returns just IDataTestBuilder<Person> (in terms of the compile-time return type) - and there's no implicit conversion from that to Person. If you change the return type of the abstract method to DataTestBuilderBase<T> then it should work - although frankly I wouldn't want to use that implicit conversion anyway. I'm generally pretty cautious about providing implicit conversions - they can often make the code less clear, as I believe they do here.

Should you really be providing AddToRepository on a builder anyway? It feels like an inappropriate action for a builder - I would expect:

Person person = new PersonBuilder { /* properties */ }
                     .Build()
                     .AddToRepository();

EDIT: Just to show what I mean about changing the return type of AddToRepository, here's a short but complete program to demonstrate. It works fine.

using System;

public abstract class BuilderBase<T>
{
    public abstract T Build();
    public abstract BuilderBase<T> AddToRepository();

    public static implicit operator T(BuilderBase<T> builder)
    {
        return builder.Build();
    }
}

public class TestBuilder : BuilderBase<string>
{
    public override string Build()
    {
        return "Built by Build()";
    }

    public override BuilderBase<string> AddToRepository()
    {
        return this;
    }
}

class Program
{
    static void Main(string[] args)
    {
        string x = new TestBuilder().AddToRepository();
        Console.WriteLine(x);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文