1

我正在寻找一种为选项类型类创建 IUserType 的方法。这是选项类型类代码:

public static class Option 
{
    public static Option<T> Some<T>(T value)
    {
        return new Option<T>(value);
    }

    public static Option<T> None<T>()
    {
        return new Option<T>();
    }
}
public class Option<T>
{
    public Option(T value)
    {
        _value = value;
        _isSome = true;
    }

    public Option()
    {
        _isSome = false;
    }

    T _value;
    bool _isSome;

    public bool IsSome
    {
        get { return _isSome; }
    }

    public bool IsNone
    {
        get { return !_isSome; }
    }

    public T Value
    {
        get { return _value; }
    }

    public T ValueOrDefault(T value)
    {
        if (IsSome)
            return Value;

        return value;
    }

    public override bool Equals(object obj)
    {
        var temp = obj as Option<T>;
        if (temp == null)
            return false;

        if (this.IsNone && temp.IsNone)
            return true;

        if (this.IsSome && temp.IsSome)
        {
            var item1 = this.Value;
            var item2 = temp.Value;
            return object.Equals(item1, item2);
        }

        return false;
    }

    public override int GetHashCode()
    {
        if (this.IsNone)
            return base.GetHashCode() + 23;

        return base.GetHashCode() + this.Value.GetHashCode() + 23;
    }
}

它基本上只是用户想要的任何类型的 T 的包装器。它最终应该映射一个可以为空的 T 版本。我一直无法找到任何有关执行此类操作的文档。

任何帮助表示赞赏。

4

1 回答 1

0

以下是我用于 IUserType 类基础的一些文章:

于 2011-05-24T15:20:34.010 回答