的TryParse空类型一般 [英] TryParse Nullable types generically

查看:273
本文介绍了的TryParse空类型一般的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了重载静态的TryParse 方法如下可空类型:诠释? 短?长?翻番?日期时间?十进制?浮?布尔?字节?字符?。下面是一些执行情况:

 保护静态布尔的TryParse(字符串输入,输出INT?值)
{
    INT outValue;
    布尔结果= Int32.TryParse(输入,输出outValue);
    值= outValue;
    返回结果;
}

受保护的静态布尔的TryParse(字符串输入,输出短路?值)
{
    短outValue;
    布尔结果= Int16.TryParse(输入,输出outValue);
    值= outValue;
    返回结果;
}

受保护的静态布尔的TryParse(字符串输入,输出多久?值)
{
    长outValue;
    布尔结果= Int64.TryParse(输入,输出outValue);
    值= outValue;
    返回结果;
}
 

的逻辑是在除了它们使用不同类型的每一个方法是相同的。这岂不是可以使用仿制药,这样我就不需要有那么多的冗余code?签名是这样的:

 布尔的TryParse< T>(字符串输入,输出的T值);
 

感谢

解决方案
  

这岂不有可能使用仿制药,这样我就不需要有那么多的冗余code?

您可以与反思做到这一点,但是这将是相对缓慢的。否则,你的可以的创建类型映射的方法来使用该类型的,但是这将是pretty的丑陋。除了别的,这绝不​​会的真正的通用 - 这将仅适用于所提供的正确的签名的的TryParse 方法,它couldn类型't在编译时是已知的。

我个人考虑更改签名和行为的方式。目前,尽管是可空的,它永远不会有在方法结束的空值,即使你返回。为什么不使返回值在失败的分析操作,返回的结果?

 受保护的静态多久? TryParseInt64(字符串输入)
{
    长outValue;
    返回Int64.TryParse(输入,输出outValue)? (长?)outValue:空;
}
 

I have written overloaded static TryParse methods for the following Nullable types: int?, short?, long?, double?, DateTime?, decimal?, float?, bool?, byte? and char?. Below is some of the implementation:

protected static bool TryParse(string input, out int? value)
{
    int outValue;
    bool result = Int32.TryParse(input, out outValue);
    value = outValue;
    return result;
}

protected static bool TryParse(string input, out short? value)
{
    short outValue;
    bool result = Int16.TryParse(input, out outValue);
    value = outValue;
    return result;
}

protected static bool TryParse(string input, out long? value)
{
    long outValue;
    bool result = Int64.TryParse(input, out outValue);
    value = outValue;
    return result;
}

The logic is the same in every method except that they use different types. Would it not be possible to use generics so that I don't need to have so much redundant code? The signature would look like this:

bool TryParse<T>(string input, out T value);

Thanks

解决方案

Would it not be possible to use generics so that I don't need to have so much redundant code?

You could do it with reflection, but that would be relatively slow. Otherwise, you could create a map from type to "method to use for that type" but it would be pretty ugly. Aside from anything else, it would never be truly generic - it would only work for types that provided a TryParse method of the right signature, which couldn't be known at compile-time.

I would personally consider changing the signature and behaviour, by the way. Currently even though the type of value is nullable, it will never have the null value at the end of the method, even if you return false. Why not make the return value the result of the parsing operation, returning null on failure?

protected static long? TryParseInt64(string input)
{
    long outValue;
    return Int64.TryParse(input, out outValue) ? (long?) outValue : null;
}

这篇关于的TryParse空类型一般的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆