C#:简化许多类似的隐式运算符方法
我有一个带有一堆隐式运算符
的类。在下面的代码中,仅使用了隐式运算符的几个示例。我正在寻找如何重构它的任何想法,而不使 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 operator
s. In the code below, only a few examples of the implicit operator
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在评论中所说,我认为最好完全替换代码。
如果这不是一个选择,您可能会考虑以下主要是外观更改:
您可能希望摆脱手动引发的异常。如果您无论如何都会在下一行中执行此操作,为什么还要检查 null 并抛出异常呢?无论如何,你都会在那里抛出异常。将 null 转换为 int 将导致异常。
为什么都是新的?不需要他们。直接投射即可。
无需双重转换您的可空值。
按预期工作
也可以工作(这就是您正在做的事情,因为您在签名中返回 null; 的函数中返回值为 DateTime?,对吗?
您的所有函数都减少到只有一行代码。这是 DateTime? 的全部优点:
重复 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.
Works as expected
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:
Repeat for int? and the like.