I've searched and have found answers that make sense when you have access to change an existing interface definition. In my case, I do not have that ability. I'm trying to create a new object with a generic that has a constraint from a generic method that does not. The following code illustrates what I'm trying to do (I can do reflection or anything necessary in MyImplementedClass, but the others are more fixed):
// This existing interface has no constraint (can't change)
public interface IExistingInterface
{
void ExistingMethod<T>();
}
public class MyImplementedClass : IExistingInterface
{
public void ExistingMethod<T>()
{
var howToDoThis = new ExistingClass<T>(); // Gives error that T must be a reference type
}
}
// This existing class has the following constraint (can't change)
public class ExistingClass<T> where T : class
{
}
ExistingMethod<int>()
is called? (imagine any other value type instead of int). For the code that works with an interface it is perfectly valid usage of theExistingMethod
as the interface has no restrictions/constraints here. – Harem