Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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
- }
- }
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement