Last active
October 13, 2018 23:29
-
-
Save martindevans/0b80876c45c6c88f0c60a2adbc1c2a87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Demo | |
{ | |
public static void DemoIt() | |
{ | |
var cat = new GenericBuilder<TypeFalse, TypeFalse>() | |
.SetAge(11) | |
.SetName("Totoro") | |
.Build(); | |
} | |
} | |
public interface ITypeBool { } | |
public class TypeTrue : ITypeBool { } | |
public class TypeFalse : ITypeBool { } | |
public class Cat | |
{ | |
public readonly string Name; | |
public readonly int Age; | |
public Cat(string name, int age) | |
{ | |
Name = name; | |
Age = age; | |
} | |
} | |
public class GenericBuilder<TName, TAge> | |
{ | |
public string Name { get; private set; } | |
public int Age { get; private set; } | |
public GenericBuilder() | |
{ | |
} | |
public GenericBuilder<TypeTrue, TAge> SetName(string name) | |
{ | |
return new GenericBuilder<TypeTrue, TAge> { Name = name, Age = Age }; | |
} | |
public GenericBuilder<TName, TypeTrue> SetAge(int age) | |
{ | |
return new GenericBuilder<TName, TypeTrue> { Name = Name, Age = age }; | |
} | |
} | |
public static class Ext | |
{ | |
public static Cat Build<TA, TB>(this GenericBuilder<TA, TB> builder) | |
where TA : TypeTrue | |
where TB : TypeTrue | |
{ | |
return new Cat(builder.Name, builder.Age); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment