相关疑难解决方法(0)

重写System.Object.GetHashCode的最佳算法是什么?

在.NET GetHashCode方法中,很多地方都使用.NET 方法.特别是在快速查找集合中的项目或确定相等性时.是否有关于如何GetHashCode为我的自定义类实现覆盖的标准算法/最佳实践,因此我不会降低性能?

.net algorithm hashcode gethashcode

1389
推荐指数
14
解决办法
19万
查看次数

在C#中使用反射来获取嵌套对象的属性

给出以下对象:

public class Customer {
    public String Name { get; set; }
    public String Address { get; set; }
}

public class Invoice {
    public String ID { get; set; }
    public DateTime Date { get; set; }
    public Customer BillTo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想用反射Invoice来获得一个Name属性Customer.这就是我所追求的,假设这段代码可行:

Invoice inv = GetDesiredInvoice();  // magic method to get an invoice
PropertyInfo info = inv.GetType().GetProperty("BillTo.Address");
Object val = info.GetValue(inv, null);
Run Code Online (Sandbox Code Playgroud)

当然,由于"BillTo.Address"不是Invoice该类的有效属性,因此失败.

所以,我尝试编写一个方法将字符串拆分成句点,并遍历对象寻找我感兴趣的最终值.它可以正常工作,但我对它并不完全满意:

public Object GetPropValue(String …
Run Code Online (Sandbox Code Playgroud)

c# reflection

69
推荐指数
6
解决办法
7万
查看次数

标签 统计

.net ×1

algorithm ×1

c# ×1

gethashcode ×1

hashcode ×1

reflection ×1