C#:简化许多类似的隐式运算符方法

发布于 2024-12-01 17:28:39 字数 1296 浏览 1 评论 0原文

我有一个带有一堆隐式运算符的类。在下面的代码中,仅使用了隐式运算符的几个示例。我正在寻找如何重构它的任何想法,而不使 SafeValue 类通用。有什么想法吗?

public class SafeValue {
    private readonly object value;
    public SafeValue(object value) {
        if (value == somethingSpecial) {
            value = null;
        }
        this.value = value;
    }
    public static implicit operator string(SafeValue instance) {
        return (string)instance.value;
    }
    public static implicit operator int(SafeValue instance) {
        if (instance.value == null) {
            throw new InvalidCastException("Cannot convert type");
        }
        return (int)instance.value;
    }
    public static implicit operator int?(SafeValue instance) {
        if (instance.value == null) {
            return null;
        }
        return new int?((int)instance.value);
    }
    public static implicit operator DateTime(SafeValue instance)
        if (instance.value == null) {
            throw new InvalidCastException("Cannot convert type");
        }
        return (DateTime)instance.value;
    }
    public static implicit operator DateTime?(SafeValue instance) {
        if (instance.value == null) {
            return null;
        }
        return new DateTime?((DateTime)instance.value);
    }
}

I have a class with a bunch of implicit operators. In the code below, only a few examples of the implicit operators are used. I'm looking for any ideas how to refactor this, without making the SafeValue class generic. Any ideas?

public class SafeValue {
    private readonly object value;
    public SafeValue(object value) {
        if (value == somethingSpecial) {
            value = null;
        }
        this.value = value;
    }
    public static implicit operator string(SafeValue instance) {
        return (string)instance.value;
    }
    public static implicit operator int(SafeValue instance) {
        if (instance.value == null) {
            throw new InvalidCastException("Cannot convert type");
        }
        return (int)instance.value;
    }
    public static implicit operator int?(SafeValue instance) {
        if (instance.value == null) {
            return null;
        }
        return new int?((int)instance.value);
    }
    public static implicit operator DateTime(SafeValue instance)
        if (instance.value == null) {
            throw new InvalidCastException("Cannot convert type");
        }
        return (DateTime)instance.value;
    }
    public static implicit operator DateTime?(SafeValue instance) {
        if (instance.value == null) {
            return null;
        }
        return new DateTime?((DateTime)instance.value);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我要还你自由 2024-12-08 17:28:39

正如我在评论中所说,我认为最好完全替换代码。

如果这不是一个选择,您可能会考虑以下主要是外观更改:

  • 您可能希望摆脱手动引发的异常。如果您无论如何都会在下一行中执行此操作,为什么还要检查 null 并抛出异常呢?无论如何,你都会在那里抛出异常。将 null 转换为 int 导致异常。

  • 为什么都是新的?不需要他们。直接投射即可。

  • 无需双重转换您的可空值

    日期时间? dt = (日期时间?) 日期时间.Now; 
    

按预期工作

    DateTime? dt = (DateTime?) null; 

也可以工作(这就是您正在做的事情,因为您在签名中返回 null; 的函数中返回值为 DateTime?,对吗?

您的所有函数都减少到只有一行代码。这是 DateTime? 的全部优点:

public static implicit operator DateTime?(SafeValue instance) {    
    return (DateTime?)instance.value;    
}

重复 int? 等。

As i said in the comments, i think it would be best to replace the code completely.

If that is not an option, you might consider the following, mostly cosmetic changes:

  • You might want to get rid of the manually thrown exceptions. Why check for null and throw an exception if you would do so anyway in the next line? You are throwing exceptions in there anyway. Casting null to int will result in an exception.

  • Why all the new s ? No need for them. Just cast.

  • No need to doublecast your Nullables.

    DateTime? dt = (DateTime?) DateTime.Now; 
    

Works as expected

    DateTime? dt = (DateTime?) null; 

Also works (that is what you are doing anyway, since you return null; in your function that has a return value of DateTime? in the signature, right?

All your functions get reduced to only one line of code. Here is the DateTime? one in all its glory:

public static implicit operator DateTime?(SafeValue instance) {    
    return (DateTime?)instance.value;    
}

Repeat for int? and the like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文