I've been reading the documentation here and I couldn't get where I can achieve what I need - sort of an overload for constraints on type parameters. I "technically" don't know how to call it thus I mentioned "overload". Here's an example of what I'm trying to achieve:
class Test<T> {
private Test(string x) {
}
public static Test<T> OpTest(T t) {
return new Test<T>("test string") {
Value = t,
}
}
public T Value1 {get;set;}
public string X {get;set;}
}
// Sample usage
var result = Test<SomeClass>.OpTest(instanceOfSomeClass);
And here's what I wanted have...
//... that I can do this
var result = Test<SomeClass>.OpTest(instanceOfSomeClass);
//and also sometimes this
var anotherResult = Test<SomeClass, SomeOtherClass>
.OpTest(instanceOfSomeClass, instanceOfSomeOtherClass);
I am able to achieve this by creating another class with a different constraint like this:
class Test<T, U> : Test<T> {
private Test(string x):base(x) { }
public static Test<T, U> OpTest(T t, U u) {
return new Test<T, U>("test string") {
Value1 = T,
Value2 = U,
}
}
public U Value2 {get;set;}
}
Although that works the way I wanted it to, I feel like something is wrong. Is it? Or is there a better way of doing it? Additionally, I only not feel it's wrong but I now have to duplicate (copy+paste) some logic between Test<T>
and Test<T,U>
.