我有一个班级Gen<T>
,我希望能够比较它们。以下代码无法编译,因为==无法应用于Parent和Child。有没有办法让这种比较成为可能,或者一般来说这是不好的做法?
public class Parent{
public int x;
}
public class Child:Parent{}
public class Gen<T>
where T : Parent
{
public T variable;
}
public static class Gen
{
public static bool operator ==(Gen<Parent> left, Gen<Parent> right){
if (left.variable.x == right.variable.x)
return true;
else
return false;
}
}
public void Test()
{
Gen<Parent> foo = new Gen<Parent>();
Gen<Child> bar = new Gen<Child>();
if (foo == bar)
{
...
}
}
完整的上下文如下:
Gen<T>
等于ColorSet<T>
,其中T:Color 我希望通过Color
类访问每个ColorSet<T>
,如下所示:
public class ColorSet<T> where T : Color
{
private T blue;
private T red;
private T green;
public ColorSet()
{
Red = (T)Activator.CreateInstance(typeof(T), new object[] { });
Red.Name = Values.Res.get("red");
Blue = (T)Activator.CreateInstance(typeof(T), new object[] { });
Blue.Name = Values.Res.get("blue");
Green = (T)Activator.CreateInstance(typeof(T), new object[] { });
Green.Name = Values.Res.get("green");
}
}
但有时我需要ColorSet<Color>
,有时需要ColorSet<Child>
来获取更多信息。并且应该可以将ColorSet<Color>
与ColorSet<Child>
进行比较,因为它们具有最相关的最相关信息。
答案 0 :(得分:1)
回到你原来的问题/样本:这不是很漂亮,但它有效(对于你的例子 - 我只测试了两个) 虽然它使用了反射,所以我对它并不满意:
public class Parent
{
public int x;
public Parent (int x)
{
this.x = x;
}
public override bool Equals(object o)
{
var p = o as Parent;
if (object.Equals(p, null))
return false;
return this.x == p.x;
}
public override int GetHashCode()
{
return x;
}
public static bool operator ==(Parent a, Parent b)
{
return a.Equals (b);
}
public static bool operator !=(Parent a, Parent b)
{
return !(a == b);
}
}
public class Child : Parent
{
public Child (int x)
: base(x)
{
}
}
public class Gen<T>
where T : Parent
{
public T variable;
public Gen (T x)
{
this.variable = x;
}
public override bool Equals(object o)
{
if (object.Equal(o, null)) return false;
// CAUTION: VERY DIRTY - just a quick reply to hvd - should check/remove this with test cases!
try
{
var oT = o.GetType ().GetGenericTypeDefinition ();
var tT = this.GetType ().GetGenericTypeDefinition ();
if (tT != oT)
return false;
// for example this:
// var oVar = o.GetType().GetField ("variable").GetValue (o);
// should really be
var varField = o.GetType().GetField("variable");
if (varField == null) return;
var oVar = varField.GetValue(o);
if (object.Equals(oVar, null))
return object.Equals(this.variable, null);
return this.variable.Equals (oVar);
} catch { return false; }
}
public override int GetHashCode()
{
return variable.GetHashCode();
}
public static bool operator ==(Gen<T> a, object b)
{
return a.Equals (b);
}
public static bool operator !=(Gen<T> a, object b)
{
return !(a == b);
}
}
这是你的,另一个例子:
public static void Test()
{
Gen<Parent> foo = new Gen<Parent>(new Parent(5));
Gen<Child> bar = new Gen<Child>(new Child(5));
Gen<Child> bas = new Gen<Child>(new Child(6));
if (foo == bar)
Console.WriteLine ("equal");
else
Console.WriteLine ("not-equal");
if (foo == bas)
Console.WriteLine ("equal");
else
Console.WriteLine ("not-equal");
}
顺便说一下:你真的不需要Parent
课上的(==)和(!=) - 但是它没有受到伤害
答案 1 :(得分:1)
(从评论中扩展)似乎没有必要使用泛型类。让运算符为泛型类型工作的有效方法是重新处理类型,使它们不再是通用的。
ColorSet
可以定义为
public class ColorSet {
private Color red;
private Color green;
private Color blue;
protected ColorSet(Type type) {
red = (Color)Activator.CreateType(type);
red.Name = Values.Res.get("red");
green = (Color)Activator.CreateType(type);
green.Name = Values.Res.get("red");
blue = (Color)Activator.CreateType(type);
blue.Name = Values.Res.get("red");
}
public static ColorSet FromType<T>() where T : Color {
return new ColorSet(typeof(T));
}
}
而不是new ColorSet<ExtendedColor>()
,您现在可以调用ColorSet.FromType<ExtendedColor>()
。
只要您实际上不需要在构造函数之外使用T
,这就可以正常工作。
例如,如果你有一个
public T Red { get { return red; } }
属性,您需要将其更改为
public Color Red { get { return red; } }
属性。
但是,如果你有类似的东西,并且你想保留泛型类型,那么你可以将它放在派生的泛型类中:
public class ColorSet<T> : ColorSet where T : Color {
public ColorSet<T>() : base(typeof(T)) { }
public new T Red { get { return (T)base.Red; } }
}
仍然只需要基础非通用ColorSet
类的运算符。
答案 2 :(得分:0)
public class IGen<out T>
where T : Parent
{
T Variable{ get; }
}
public class Gen<T>
: IGen<T>
where T : Parent
{
public T Variable {get;set;}
private static Func<T, T, bool> _equal;
static Gen()
{
var left = Expression.Parameter(typeof(T));
var right = Expression.Parameter(typeof(T));
var body = Expression.Equal(left, right);
var lambda = Expression.Lambda<Func<T, T, bool>>(body, left, right);
_equal = lambda.Compile();
}
public static bool operator ==(Gen<T> left, Gen<T> right)
{
return _equal(left.Variable, right.Variable);
}
public static bool operator ==(Gen<T> left, IGen<T> right)
{
return _equal(left.Variable, right.Variable);
}
public static bool operator ==(IGen<T> left, Gen<T> right)
{
return _equal(left.Variable, right.Variable);
}
}