Как абстрактно сортировать объекты в строго типизированные словари?C#

Место общения программистов 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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Как заставить приводить типизированные словари, чтобы избежать несовместимых типов при назначении
    Anonymous » » в форуме Python
    0 Ответы
    22 Просмотры
    Последнее сообщение Anonymous
  • Строго типизированные перечисления для Python (mypy)
    Anonymous » » в форуме Python
    0 Ответы
    11 Просмотры
    Последнее сообщение Anonymous
  • Строго типизированные перечисления для Python (mypy)
    Anonymous » » в форуме Python
    0 Ответы
    13 Просмотры
    Последнее сообщение Anonymous
  • Строго типизированные утверждения в ASP.NET Core
    Anonymous » » в форуме C#
    0 Ответы
    8 Просмотры
    Последнее сообщение Anonymous
  • Строго типизированные утверждения в ASP.NET Core
    Anonymous » » в форуме C#
    0 Ответы
    20 Просмотры
    Последнее сообщение Anonymous

Вернуться в «C#»