gpt4 book ai didi

c# - 一组可以相互比较的自定义类型

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:06 25 4
gpt4 key购买 nike

我想构建一组基于表示不同类型值的接口(interface)的对象:

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
{

}

最佳答案

我认为您基本上是在寻找变体类型 的C# 版本。我现在能想到的最接近的东西是 C# dynamics:

  public class DynamicComparer : IComparer<dynamic>
{ // ...

不幸的是,那是非法的,所以你可以求助于

  public class DynamicComparer : IComparer<object>
{

并在其中实现您想要的逻辑。

头脑 Storm :

我刚刚想到的另一种方法是提供一个带有隐式转换的统一类 - 是的我不推荐这个 - 但我确实喜欢想出最有创意的方式来回答原始问题:

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");
}
}

关于c# - 一组可以相互比较的自定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7930243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com