English 中文(简体)
使用通用的含蓄操作器对这种情况有什么错误?
原标题:what is wrong with this using generic implicit operator?

如果我使用非通用建筑类别中的含蓄操作器,那么每件事情都会是:

 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

问题是什么?

最佳回答

数据验证局;T>,然后运行——尽管坦率地说,数据验证局应当发挥作用。 我不想利用这种暗中转换。 我通常对提供隐含的转换持谨慎态度,因为我相信,这样做常常会使守则不明确。

您是否真的在builder <>/em>上提供<编码>。 它感到对建筑商采取不适当的行动,我希望:

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

EDIT:仅表明我对更改<代码> /托存的返回类型的含义,这里有一个简短但完整的证明方案。 罚款。

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);
    }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签