I actually come up with the following implementation
bool DoesNamedDataSlotsExist(string name) { try { Thread.AllocateNamedDataSlot(name); } catch { return true; } return false; }
The obvious problem is here: if some code calls DoesNamedDataSlotExist() twice, it will generate false first, then true (which could be optimized if I used Thread.FreeNamedDataSlot() ...)
But is there a better way?
EDIT
source GetNamedDataSlot
public LocalDataStoreSlot GetNamedDataSlot(string name) { LocalDataStoreSlot slot2; bool tookLock = false; RuntimeHelpers.PrepareConstrainedRegions(); try { Monitor.ReliableEnter(this, ref tookLock); LocalDataStoreSlot slot = (LocalDataStoreSlot) this.m_KeyToSlotMap[name]; if (slot == null) { return this.AllocateNamedDataSlot(name); } slot2 = slot; } finally { if (tookLock) { Monitor.Exit(this); } } return slot2; }
Somehow I will need to access this.m_KeyToSlotMap ...
source share