[英]C# code re-use public class Foo<T> method parameters
我希望有人可以帮助我解决问题。 我正在尝试制作一个新的Type类:
public class Foo<T>
其唯一方法的工作是遍历另一个类的属性。 大约有20个类别,定义了不同数量的属性:
public class Bar {
public int Id { get; set; }
public string Name { get; set; }
}
public class Shirt {
public string Color { get; set; }
public string Size { get; set; }
public string Brand { get; set; }
}
public class Pants {
public string Color { get; set; }
public string Waist { get; set; }
public string Length { get; set; }
public string Inseam { get; set; }
}
而公共类Foo的方法是:
public class Foo<T> {
public string FooMethod(T before, T after /* other variables? */) {
// compare before & after
}
}
每个命名相同的类将两次进入Foo
的主方法,但具有不同的属性值。 所以Bar
之前的ID和名称可能是1 & "Bar"
,而Bar
后的ID和名称可能是2 & "BarBar"
。
从这里开始,将要进行其他操作以比较before和after中的每个属性。 但是,由于每个类中属性的数量各不相同,并且我只想使用一种方法,所以我感到两难。
如果要在FooMethod中比较多种类型的对象,则可以在所有类(Bar,Shirt,Pants ...)中覆盖Equals和GetHashcode方法,并在那里进行比较。 这样,您的FooMethod将只在before.Equals(after)
进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.