Как абстрактно сортировать объекты в строго типизированные словари? ⇐ C#
Как абстрактно сортировать объекты в строго типизированные словари?
Some context: I am working on a networking solution. This includes an ID system (the SharedID class) so that the client can easily refer to any shared object by specifying the ID.
I want to keep track of all objects (this part is easy), but ideally I would also like to maintain a strongly typed dictionary for every type of object, so that I don't have to edit the SharedID class every time I introduce a new type to the program.
The closest thing I could find is in the code block (I've omitted the GetObjectByID use case). As you can see, it does not return a strongly typed dictionary, but simply a dictionary with objects.
public static class SharedID { static long IDCounter = 1; static Dictionary typedObjects = new Dictionary(); public static long Assign(object caller) { if(!typedObjects.ContainsKey(caller.GetType())) { typedObjects[caller.GetType()] = new Dictionary(); } typedObjects[caller.GetType()][IDCounter] = caller; return IDCounter++; } public static Dictionary GetAllWithType(Type type) { return typedObjects[type]; } } Maybe there is a way to cast the dictionary to the correct value? "unsafe" approaches would be OK, I know that the objects are all of the correct type.
Performance is one of my main priorities so solutions like filtering the dictionary every time are not sufficient.
I have tried using generics to tackle this problem, but I think that doesn't work with runtime types, so I would still be stuck having to add types manually every time.
Источник: https://stackoverflow.com/questions/780 ... abstractly
Some context: I am working on a networking solution. This includes an ID system (the SharedID class) so that the client can easily refer to any shared object by specifying the ID.
I want to keep track of all objects (this part is easy), but ideally I would also like to maintain a strongly typed dictionary for every type of object, so that I don't have to edit the SharedID class every time I introduce a new type to the program.
The closest thing I could find is in the code block (I've omitted the GetObjectByID use case). As you can see, it does not return a strongly typed dictionary, but simply a dictionary with objects.
public static class SharedID { static long IDCounter = 1; static Dictionary typedObjects = new Dictionary(); public static long Assign(object caller) { if(!typedObjects.ContainsKey(caller.GetType())) { typedObjects[caller.GetType()] = new Dictionary(); } typedObjects[caller.GetType()][IDCounter] = caller; return IDCounter++; } public static Dictionary GetAllWithType(Type type) { return typedObjects[type]; } } Maybe there is a way to cast the dictionary to the correct value? "unsafe" approaches would be OK, I know that the objects are all of the correct type.
Performance is one of my main priorities so solutions like filtering the dictionary every time are not sufficient.
I have tried using generics to tackle this problem, but I think that doesn't work with runtime types, so I would still be stuck having to add types manually every time.
Источник: https://stackoverflow.com/questions/780 ... abstractly
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение