I have the following scenario:
public interface IParam
{
Object Value {get;set;}
}
public abstract class BaseClass: IParam
{
public virtual object Value {get;set;}
}
public class IntSubClass:BaseClass
{
public override object Value {get;set;}
}
To the outside world, this interface is exposed. All properties are accessed via IParam. The drawback of this approach is a lot of casting issue.
IParam dummy = ParameterList[abc];
int index = (int)dummy.Value;
Request please advise how this problem can be solved using generics. To the outside, I can only expose generic interface.
If we assume the parameters can be of different types, then simply: no.
Oh, you can introduce generics… but that just changes things from
to:
doesn’t really help much. The only other option is implicit conversion operators, which a: don’t work on
interfaces, and b: are not very clear / obvious.Frankly, I’d just use the cast. It isn’t a problem. A box/unbox is not hugely expensive, and the code is clear in its purpose.