[英]C# Best way to compare fields of class
我需要比较Product,AdvancedProduct(以及从Product类继承的其他类)的可能性。如何更好地实现字段的层次检查? 例如,我想检查两个AdvancedProduct类,首先检查基本类Product的字段,然后检查AdvancedProduct的其他字段,并以某种形式返回它们之间的某种形式的更改(???)(也许是PChanges ???类)。 是否有合适的模板? 如何做到所有这些,但要灵活地用于后续使用?
public class Product
{
public string ID;
public string Name;
public Product(string id, string name)
{
this.ID = id;
this.Name = name;
}
}
public class AdvancedProduct : Product
{
public string CurrentVersion;
public AdvancedProduct(string id, string name, string version)
: base(id, name) { }
}
public class PChanges
{
public bool NameChanged = false;
public bool VersionChanged = false;
public PChanges() { }
}
public class ProductComparer
{
PChanges changes = new PChanges();
public ProductComparer() { }
public PChanges Compare(AdvancedProduct p1, AdvancedProduct p2)
{
if (p1.Name != p2.Name)
changes.NameChanged = true;
if (p1.CurrentVersion != p2.CurrentVersion)
changes.VersionChanged = true;
return changes;
}
}
有一个不错的.NET库,称为Compare .NET Objects。 它可以用于比较复杂的对象,而无需编写比较代码。 它也是非常可定制的-您可以告诉它排除某些属性,包括其他属性,等等。它可以比较平面对象和对象层次结构。 您可以从CodePlex- http: //comparenetobjects.codeplex.com/下载。
最好是ProductComparer实现IEqulaityComparer
有关更多详细信息,请参见给定链接中的示例。
根据Uzzy的回答,看起来它可以扩展以跟踪更改。 是的,这是不好的做法,但是对于小型应用程序来说就足够了。 例:
public class ProductComparer : IEqualityComparer<Product>{
private PChanges change;
public PChanges Changes{ get { return change; } }
public bool Equals(Product p1, Product p2){
PChanges newChange = new PChanges();
bool equal = true;
if(p1.Name != p2.Name){
newChange.NameChange = true;
equal = false;
}
this.change = newChange;
return equal;
}
}
编辑:
我误解了可扩展字段比较的要求。 如果是这种情况,那么装饰器模式最适合您。 假设所有其他Product类都应继承自Product类。
public class ProductComparer{
public virtual void TrackChange(Product p1, Product p2, ref PChange change){
if(p1.Name != p2.Name){
change.NameChange = true;
}
// other base validation
}
}
public class AdvancedProductComparer : ProductComparer{
public AdvancedProductComparer(ProductComparer baseComparer){
this.baseComparer = baseComparer;
}
ProductComparer baseComparer;
public override void TrackChange(Product p1, Product p2, ref PChange change){
baseComparer.Compare(p1, p2, ref change);
if( ((AdvancedProduct)p1).CurrentVersion != ((AdvancedProduct)p2).CurrentVersion){
change.CurrentVersion = true;
}
}
}
public class ProductComparerService{
public ProductComparerService(ProductComparer comparer){
this.comparer = comparer;
}
ProductComparer comparer;
private PChanges change;
public PChanges Changes{ get { return change; } }
public bool Equals(Product p1, Product p2){
PChanges newChange = new PChanges();
comparer.Compare(p1,p2, ref newChange);
this.change = newChange;
return (newChange.CurrentVersion || newChange.NameChange);
}
}
用法:
ProductComparer pCompare = new ProductComparer();
AdvancedProductComparer advCompare = new AdvancedProductComparer(pCompare);
ProductComparerService service = new ProductComparerService(advCompare);
if( service.Equals(p1,p2) ){
PChange change = service.Change;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.