如果我使用非通用建筑类别中的含蓄操作器,那么每件事情都会是:
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();
the error is :cannot convert IDataTestBuilder to Person
问题是什么?