I have given a very similar solution before but using a template you can generate multiple overloads that use the different types that you want (at compiletime).
<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="NamespaceProofOfConcept" #>
<#@ assembly name="$(TargetPath)" #>
<# Type[] types = new[] {
typeof(byte), typeof(short), typeof(int),
typeof(long), typeof(float), typeof(double),
typeof(bool), typeof(DateTime), typeof(char),
typeof(string)
};
#>
using System;
namespace NamespaceProofOfConcept {
public partial class Test {
<# foreach (var type in types) {
#>
public Test(<#= type.Name #> value) {
doConstructorStuff(value);
}
<#
} #>
private void doConstructorStuff(object o){
}
}
}
This will generate the following class:
using System;
namespace NamespaceProofOfConcept {
public partial class Test {
public Test(Byte value) {
doConstructorStuff(value);
}
public Test(Int16 value) {
doConstructorStuff(value);
}
public Test(Int32 value) {
doConstructorStuff(value);
}
public Test(Int64 value) {
doConstructorStuff(value);
}
public Test(Single value) {
doConstructorStuff(value);
}
public Test(Double value) {
doConstructorStuff(value);
}
public Test(Boolean value) {
doConstructorStuff(value);
}
public Test(DateTime value) {
doConstructorStuff(value);
}
public Test(Char value) {
doConstructorStuff(value);
}
public Test(String value) {
doConstructorStuff(value);
}
private void doConstructorStuff(object o){
}
}
}
Now you can only call this constructor with the types you defined (which are essentially the VB.NET primitive types).
You can add functionality to this by creating your own partial class which defines the behaviour:
namespace NamespaceProofOfConcept
{
public partial class Test
{
public void DoAThing()
{
System.Console.WriteLine("custom method!");
}
}
}
And you can test it out with the following code (which outputs "custom method!"):
static void Main(string[] args)
{
Test obj = new Test(true);
obj.DoAThing();
Console.ReadKey();
}
If you want even more "security", you can define the parameterless constructor as private inside your T4 template so it cannot be added unless the template is modified.