使用通用隐式运算符有什么问题?
如果我在非泛型构建器类中使用隐式运算符,则一切正常:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AddToRepository
仅返回IDataTestBuilder
(就编译时返回类型而言) - 并且没有从 that 到的隐式转换>人
。如果将抽象方法的返回类型更改为 DataTestBuilderBase您真的应该在构建器上提供
AddToRepository
吗?对于构建器来说,这感觉像是一个不适当的操作 - 我希望:编辑:只是为了展示我关于更改
AddToRepository
返回类型的含义,这里有一个简短但完整的程序来演示。效果很好。AddToRepository
returns justIDataTestBuilder<Person>
(in terms of the compile-time return type) - and there's no implicit conversion from that toPerson
. If you change the return type of the abstract method toDataTestBuilderBase<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: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.