如何防范与DateTime.TryParseExact
(如果可能的话,获取解析值)?下面的代码不起作用。
[<EntryPoint>]
let main args =
let argList = args |> List.ofSeq
match argList with
| "aaa" :: [] -> aaa.main "aaa"
| "bbb" :: [] -> bbb.main "bbb"
| "ccc" :: yyyymm :: [] when DateTime.TryParseExact
(yyyymm, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None)->
ccc.main "ccc" yyyymm
您可以使用mutable
:
let mutable dt = Unchecked.defaultof<_>
match argList with
| "ccc" :: yyyymm :: [] when
DateTime.TryParseExact(yyyymm,
"yyyyMM",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
&dt) -> ...
但活跃模式使匹配更加清晰:
let (|DateTimeExact|_|) (format: string) s =
match DateTime.TryParseExact(s, format, CultureInfo.InvariantCulture, DateTimeStyles.None) with
| true, d -> Some d
| _ -> None
match argList with
| "ccc" :: DateTimeExact "yyyyMM" yyyymm :: [] -> ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)