To simplify using a specific type of dictionary I have derived a class from the generic Dictionary<> to handle assorted elements derived from a common base class:
//my base class holding a value
public abstract class A{ public int aValue; }
//derived classes that actually are stuffed into the dictionary
public class B : A {...}
public class C : A {...}
//wrapper class for dictionary
public class MyDict : Dictionary<string, A>;
//my class using the dictionary
public class MyClass {
public MyDict dict = new MyDict();//use an instance of MyDict
public MyClass() { ... //fill dict with instances of B and C }
//function to return all elements of dict having a given value
public MyDict GetSubSet(int testVal) {
var ret = dict.Where(e => e.Value.aValue == testVal).
ToDictionary(k => k.Key, k => k.Value);
return (MyDict) ret; // <- here I get a runtime InvalidCastException
}
}
Before wrapping the generic Dictionary in the MyDict-class the cast succeeded (if I replace all instances of MyDict
with Dictionary<string,int>
, the code works fine, even without casting in the return statement).
I have also tried to cast the result using return ret as MyDict;
but that will return a null value. Casting through object
like this: return (MyDict) (object) ret;
also fails with an InvalidCastException.
Anybody have an idea how to cast/convert the return value correctly?