简体   繁体   中英

Mark standard library method as `Obsolete` or otherwise "hiding" it

String.Contains optionally takes a StringComparison enum (eg StringComparison.OrdinalIgnoreCase ). I keep forgetting to specify IgnoreCase , so I want to mark that method as [Obsolete] so I see it in Warnings so I remember to specify the kind of StringComparison .

I'm open to other solutions too, like doing something to "hide" that method and prevent it from being called. The ideal solution doesn't involve Roslyn analyzers though, as I'm using F#.

This solution is interesting, but obviously not ideal.

I would use an Extension Method. Just put it somewhere that's available to all your code, and declare it directly in the System namespace. (I'm not fluent in F#, but this should work.)

namespace System

type String with
  member CaselessContains(str) = String.Contains(str, StringComparison.OrdinalIgnoreCase)

Ionide for VS Code supports analyzers . You'd have to write one for the methods you want to disallow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM