我想构建一组构建在界面顶部的对象,这些对象代表不同类型的值:
integers
strings
datetime
这些对象将能够执行以下操作:
IData intValue = new IntData();
IData intValue2 = new IntData();
bool result = intValue.EqualsTo(intValue2);
IData dateTimeData = new DateTimeData();
IData dateTimeData2 = new DateTimeData();
bool result = dateTimeData.GreaterThan(dateTimeData2);
所以我需要一个界面,但我如何设置比较等的能力呢? 如果类型是IEnumerable呢?
public interface IData
{
}
答案 0 :(得分:0)
我认为您基本上正在寻找变体类型的C#版本。我现在能想到的最接近的事情是C#dynamic
s:
public class DynamicComparer : IComparer<dynamic>
{ // ...
不幸的是,这是非法的,所以你可以诉诸
public class DynamicComparer : IComparer<object>
{
并在其中实现您想要的逻辑。
我刚刚想到的另一种方法是提供一个带有隐式转换的统一类 - 是的我不推荐这个 - 但我很想想最有创意的方式回答原始问题:
public class MyData : IData, IComparable<MyData>
{
public static implicit operator MyData(string s) { /* ... */ }
public static implicit operator MyData(DateTime dt) { /* ... */ }
public static implicit operator MyData(int dt) { /* ... */ }
// implement IComparable<MyData> members...
// e.g.:
private dynamic variant_data; // or, the desired default representation
// for comparisons, e.g. string?
}
更新以下是一个半完整的示例,说明如何滥用隐式转换以达到您想要的效果(几乎透明地比较析取类型): https://ideone.com/WwI87 < /强>
using System;
using System.Collections.Generic;
public class Program
{
public class MyData : IEquatable<MyData>, IComparable<MyData>
{
public static implicit operator MyData(string s) { var data = new MyData(); /* TODO */ return data; }
public static implicit operator MyData(DateTime dt) { var data = new MyData(); /* TODO */ return data; }
public static implicit operator MyData(int dt) { var data = new MyData(); /* TODO */ return data; }
// implement IComparable<MyData> members...
// implement IEquatable<MyData> members...
// override object.Equals()
// override object.GetHashCode()
public static bool operator <(MyData a, MyData b) { return Comparer<MyData>.Default.Compare(a,b) == -1; }
public static bool operator >(MyData a, MyData b) { return Comparer<MyData>.Default.Compare(a,b) == +1; }
// e.g.:
// the desired default representation for comparisons, e.g. string?
// use 'dynamic' on C# 4.0 and beyond
private object/*dynamic*/ variant_data;
public int CompareTo(MyData other)
{
// TODO implement your own logic
return GetHashCode().CompareTo(null != other ? other.GetHashCode() : 0);
}
// TODO implement your own equality logic:
public bool Equals(MyData other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other.variant_data, variant_data);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (MyData)) return false;
return Equals((MyData) obj);
}
public override int GetHashCode() { return (variant_data != null ? variant_data.GetHashCode() : 0); }
public static bool operator ==(MyData left, MyData right) { return Equals(left, right); }
public static bool operator !=(MyData left, MyData right) { return !Equals(left, right); }
}
public static void Main(string[] args)
{
var cmp = Comparer<MyData>.Default;
string s = "123";
int i = 234;
DateTime dt = DateTime.Now;
if (-1 == cmp.Compare(s, i)) Console.WriteLine("s < i");
if (+1 == cmp.Compare(dt, i)) Console.WriteLine("dt > i");
// or even:
if ((MyData) s > i) Console.WriteLine("s > i");
if ((MyData) dt< i) Console.WriteLine("dt < i");
}
}