有没有办法获取实例的唯一标识符?
GetHashCode()对于指向同一实例的两个引用是相同的.但是,两个不同的实例可以(很容易)获得相同的哈希码:
Hashtable hashCodesSeen = new Hashtable();
LinkedList<object> l = new LinkedList<object>();
int n = 0;
while (true)
{
object o = new object();
// Remember objects so that they don't get collected.
// This does not make any difference though :(
l.AddFirst(o);
int hashCode = o.GetHashCode();
n++;
if (hashCodesSeen.ContainsKey(hashCode))
{
// Same hashCode seen twice for DIFFERENT objects (n is as low as 5322).
Console.WriteLine("Hashcode seen twice: " + n + " (" + hashCode + ")");
break; …Run Code Online (Sandbox Code Playgroud) 视觉上,以下两个片段都生成相同的UI.那么为什么有2个控件..
Snippet1
<TextBlock>Name:</TextBlock>
<TextBox Name="nameTextBox" />
Run Code Online (Sandbox Code Playgroud)
Snippet2
<Label>Name:</Label>
<TextBox Name="nameTextBox" />
Run Code Online (Sandbox Code Playgroud)
(好吧,我自己会回答这个问题......认为这是我今天从编程WPF学到的一个有用的小问题)
我需要自动化Winform应用程序.如何设置AutomationID(或AutomationName)像本文中的XAML 一样?
从这个堆栈溢出文章看,答案似乎是否定,除非我将应用程序切换到WPF应用程序(因此我可以使用XAML来定义控件).
我尝试过这种天真的方法:
AutomationElement formAutomation = AutomationElement.FromHandle(this.Handle);
formAutomation.Current.Name = "SandboxResponseDialogName";
formAutomation.Current.ClassName = "SandboxResponseDialogClassName";
formAutomation.Current.AutomationId = "SandboxResponseDialogID;
Run Code Online (Sandbox Code Playgroud)
但是在控制构造函数的这一点上,这些Automation属性只有getter; 没有二传手.