As you've already discovered pointer types in C# can't be used as the key type of a Dictionary<TKey, TValue>
. A IntPtr
value can though and every void*
is convertible to IntPtr
. Hence I would use Dictionary<IntPtr, MyClass<T>>
and just map the void*
values into IntPtr
.
unsafe class Container<T> {
Dictionary<IntPtr, MyClass<T>> m_map;
void Add(void* key, MyClass<T> value) {
IntPtr ptr = (IntPtr)key;
m_map.Add(ptr, value);
}
bool TryGetValue(void* key, out MyClass<T> value) {
IntPtr ptr = (IntPtr)key;
return m_map.TryGetValue(ptr, out value);
}
}