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?
You get an invalid cast exception because the result of ToDictionary is not MyDict. In order to fix this problem, add a constructor to MyDict that takes an IDictionary<string,A>, and return the result of calling that constructor from your GetSubSet method:
public class MyDict : Dictionary<string, A> {
public MyDict() {
// Perform the default initialization here
...
}
public MyDict(IDictionary<string,A> dict): base(dict) {
// Initialize with data from the dict if necessary
...
}
}
...
public MyDict GetSubSet(int testVal) {
var ret = dict.Where(e => e.Value.aValue == testVal).
ToDictionary(k => k.Key, k => k.Value);
return new MyDict(ret);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With