public interface IRule <T>
{
T Execute();
}
public class Rule : IRule<int>
{
public int Execute()
{
return 10;
}
}
IRule<int> rule = new Rule();
var genericRule = (IRule<object>) rule;
//Exception has occurred: CLR/System.InvalidCastException An exception of ype 'System.InvalidCastException' occurred in test.dll but was not handled in user code: 'Unable to cast object of type 'test.GenericCasting.Rule' to type test.GenericCasting.IRule`1[System.Object]'.'
In business logic, I need to load all Rules objects through reflection and don't have the knowledge of the type used in the Rule object. Any idea how to solve this?