比较C#中两个泛型类类型的对象
本文关键字:泛型类 两个 类型 对象 比较 | 更新日期: 2023-09-27 18:00:22
我有一个家庭作业,我必须通过两个对象的一个属性来比较它们,但由于我在链表上使用泛型类型,我必须存储对象,所以我无法真正访问对象的特定属性(我使用的是一个接口,所有的类都实现了该接口)因此,我有一个主要的接口,以及一些继承接口属性(如Age)的类(即class1、class2),或者任何易于比较的数字。在Main()中,我刚刚创建了一个新的LinkedList,并创建了一些class1、class2对象。我用SortedListAddElement方法将它们放在有序列表中,但我一直停留在它应该通过int属性比较元素的部分。从那时起,我应该如何处理这些源代码?我应该把第一行改为:class List:IEnumerable,IComparable,IMajorInterface吗?或者更改CompareTo()中的某些内容?请帮忙!
interface IMajorInterface
{
string Name{ get; }
int Age { get; set; }
}
class List<T> : IEnumerable<T>, IComparable<T>
{
ListElement<T> head;
public List()
{
head = null;
}
public void SortedListAddElement(T newValue)
{
ListElement<T> new = new ListElement<T>(newValue);
ListElement<T> p = head;
ListElement<T> e = null;
while (p != null /* && this is the part where I should compare the p and new objects by their property, for example the Age property, like: p.Age < new.Age */)
{
e = p;
p = p.Next;
}
if (e == null)
{
new.Next = head;
head = new;
}
else
{
new.Next = p;
e.Next = new;
}
}
public int CompareTo(T obj)
{
if (obj == null)
{
return -1;
}
else
{
if (obj is T)
{
T obj2 = obj as T;
return obj.Age.CompareTo(obj2.Age);
}
else
{
return -1;
}
}
}
有几个选项可以解决这个问题。这里有一种方法。首先,使您的主要界面具有可比性:
interface IMajorInterface : IComparable<IMajorInterface>
{
string Name{ get; }
int Age { get; set; }
}
然后在列表类实现中约束泛型类型参数:
class List<T> : IEnumerable<T>
where T : IComparable<T>
{
// ... Code omitted for brevity.
}
然后在具体的主类(class1、class2)中实现IComparable
接口。也许最好为它们实现一个基类,在那里实现接口,并从该基类派生class1和class2等。也许完全放弃IMajorInterface
,只创建一个抽象的Major
基类会更好。
或者,由于您只将列表用于专业,因此可以将泛型列表参数约束为IMajorInterface
或Major
基类本身。这将是最简单(也是最不灵活)的解决方案:
class List<T> : IEnumerable<T>
where T : IMajorInterface // No need to implement IComparable<T> in any class.
{
// ... Code omitted for brevity.
}
最后,作为另一种方法,您可以在列表的构造函数上为其提供IComparer<T>
,就像System.Collections.Generic
类所做的那样。
我希望它能有所帮助。编码快乐!
您可以实现IComparer。
class ageComparer : IComparer<Person>
{
public int Compare(Person x, Person y)
{
return x.Age - y.Age;
}
}
看看这篇文章http://www.blackwasp.co.uk/IComparer_2.aspx
以下是c-sharpcorner上一篇比较泛型类型的文章:
http://www.c-sharpcorner.com/UploadFile/1a81c5/compare-2-objects-of-generic-class-type-in-C-Sharp/
快速代码片段:
static bool Compare<T>(T Object1, T object2)
{
//Get the type of the object
Type type = typeof(T);
//return false if any of the object is false
if (Object1 == null || object2 == null)
return false;
//Loop through each properties inside class and get values for the property from both the objects and compare
foreach (System.Reflection.PropertyInfo property in type.GetProperties())
{
if (property.Name != "ExtensionData")
{
string Object1Value = string.Empty;
string Object2Value = string.Empty;
if (type.GetProperty(property.Name).GetValue(Object1, null) != null)
Object1Value = type.GetProperty(property.Name).GetValue(Object1, null).ToString();
if (type.GetProperty(property.Name).GetValue(object2, null) != null)
Object2Value = type.GetProperty(property.Name).GetValue(object2, null).ToString();
if (Object1Value.Trim() != Object2Value.Trim())
{
return false;
}
}
}
return true;
}