jeudi 26 mars 2020

Recursive inheritance as parameter

I'm looking for a solution to this recursive builder. I prefer the non-working constructor since it will help if Test class have tons of class members.

class Test
    {
        int value;
        protected Test(int value) 
        {
            this.value = value;
        }

        protected Test(TestBuilder<?> builder) // What to do here with ?
        {

        }

        class TestBuilder<T> where T : TestBuilder<T>
        {
            private int value;

            public T Value(int value)
            {
                this.value = value;
                return (T)this;
            }

            public Test BuildWorks()
            {
                return new Test(value);
            }

            public Test BuildDoesNotWork()
            {
                return new Test(this);
            }
        }
    }

Does anyone have any suggestion? C# doesn't seem to have a wildcard for generic type.

Thanks.

Aucun commentaire:

Enregistrer un commentaire