Bellow is simplified version of the code I have:
public interface IControl<T>
{
T Value { get; }
}
public class BoolControl : IControl<bool>
{
public bool Value
{
get { return true; }
}
}
public class StringControl : IControl<string>
{
public string Value
{
get { return ""; }
}
}
public class ControlFactory
{
public IControl GetControl(string controlType)
{
switch (controlType)
{
case "Bool":
return new BoolControl();
case "String":
return new StringControl();
}
return null;
}
}
The problem is in GetControl method of ControlFactory class. Because it returns IControl and I have only IControl<T> that is a generic interface. I cannot provide T because in Bool case it’s going to bool and in String case it’s going to be string.
Any idea what I need to do to make it work?
Just derive
IControl<T>fromIControl.UPDATE
If I missunterstood you, and you don’t want a non-generic interface, you will have to make the method
GetControl()generic, too.Now you have the problem that the new controls cannot be implicitly casted to
IControl<T>and you would have to make this explicit.UPDATE
Changed the cast from
as IControl<T>to(IControl<T>). This is prefered because it will cause an exception if there is a bug whileas IControl<T>silently returnsnull.