Skip to content

Instantly share code, notes, and snippets.

@martindevans
Last active October 13, 2018 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martindevans/0b80876c45c6c88f0c60a2adbc1c2a87 to your computer and use it in GitHub Desktop.
Save martindevans/0b80876c45c6c88f0c60a2adbc1c2a87 to your computer and use it in GitHub Desktop.
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