[英]Converting Object.GetHashCode() to Guid
I need to assign a guid to objects for managing state at app startup & shutdown It looks like i can store the lookup values in a dictionary using 我需要为应用程序启动和关闭时管理状态的对象分配一个guid看起来我可以使用字典将查找值存储在字典中
dictionary<int,Guid>.Add(instance.GetHashCode(), myGUID());
are there any potential issues to be aware of here ? 这里有什么潜在的问题需要注意吗?
This does NOT need to persist between execution runs, only the guid like so 这不需要在执行运行之间持续存在,只需要像这样的guid
before app terminate, gethashcode() and lookup guid to update() or insert() into persistence engine USING GUID 在应用程序终止之前,gethashcode()和查找guid以更新()或插入()到持久性引擎USING GUID
only assumption is that the gethashcode() remains consistent while the process is running 唯一的假设是gethashcode()在进程运行时保持一致
also gethashcode() is called on the same object type (derived from window) 在相同的对象类型(从窗口派生)上调用gethashcode()
List<WeakReference>
at startup List<WeakReference>
Hope the above makes it clear. 希望上面说清楚。
So now the question is - why not just store the HashCode (of usercontrol only) instead of WeakReference
and eliminate the test for null reference while iterating thru the list 所以现在的问题是 - 为什么不只是存储HashCode(仅用户控件)而不是
WeakReference
并在通过列表迭代时消除空引用的测试
update 3 - thanks all, going to use weakreference finally 更新3 - 谢谢所有,最后将使用弱引用
You appear to be assuming that a hash code will be unique. 您似乎假设哈希码是唯一的。 Hash codes don't work like that.
散列码不起作用。 See Eric Lippert's blog post on Guidelines and rules for GetHashCode for more details, but basically you should only ever make the assumptions which are guaranteed for well-behaving types - namely the if two objects have different hash codes, they're definitely unequal.
有关更多详细信息,请参阅Eric Lippert 关于GetHashCode指南和规则的博客文章,但基本上您应该只为保持良好行为的类型做出假设 - 即如果两个对象具有不同的哈希码,则它们肯定是不相等的。 If they have the same hash code, they may be equal, but may not be.
如果它们具有相同的哈希码,则它们可能相等,但可能不相同。
EDIT: As noted, you also shouldn't persist hash codes between execution runs. 编辑:如上所述,您也不应该在执行运行之间保留哈希码。 There's no guarantee they'll be stable in the face of restarts.
面对重启,无法保证他们会保持稳定。 It's not really clear exactly what you're doing, but it doesn't sound like a good idea.
目前还不是很清楚你正在做什么,但这听起来不是一个好主意。
EDIT: Okay, you've now noted that it won't be persistent, so that's a good start - but you still haven't dealt with the possibility of hash code collisions. 编辑:好的,你现在已经注意到它不会持久,所以这是一个好的开始 - 但你仍然没有处理哈希码冲突的可能性。 Why do you want to call
GetHashCode()
at all? 你为什么要调用
GetHashCode()
? Why not just add the reference to the dictionary? 为什么不添加对字典的引用?
Use GetHashCode to balance a hash table . 使用GetHashCode来平衡哈希表 。 That's what it's for.
这就是它的用途。 Do not use it for some other purpose that it was not designed for;
不要将它用于其他非设计用途的目的; that's very dangerous.
那很危险
The quick and easy fix seems to be 快速简便的修复似乎是
var dict = new Dictionary<InstanceType, Guid>();
dict.Add(instance, myGUID());
Of course you need to implement InstanceType.Equals correctly if it isn't yet. 当然,如果还没有正确实现InstanceType.Equals。 (Or implement
IEQuatable<InstanceType>
) (或实现
IEQuatable<InstanceType>
)
Possible issues I can think of: 我能想到的可能问题:
Note - Jon said this more elegantly ( see above ) 注 - Jon更优雅地说( 见上文 )
Since this is for WPF controls, why not just add the Guid as a dependency proptery? 由于这是针对WPF控件的,为什么不将Guid添加为依赖项预测呢? You seem to already be iterating through the user controls, in order to get their hash codes, so this would probably be a simpler method.
您似乎已经在迭代用户控件,以获取其哈希码,因此这可能是一个更简单的方法。
If you want to capture that a control was removed and which Guid it had, some manager object that subscribes to closing/removed events and just store the Guid and a few other details would be a good idea. 如果你想捕获一个控件被删除以及它有哪个Guid,那么订阅关闭/删除事件的一些管理器对象只是存储Guid和其他一些细节将是一个好主意。 Then you would also have an easier time to capture more details for analysis if you need.
然后,如果需要,您还可以更轻松地捕获更多详细信息以进行分析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.