異なるタイプ変換
<code class="language-csharp">public sealed class AuthenticationMethod { private readonly string name; private readonly int value; public static readonly AuthenticationMethod FORMS = new AuthenticationMethod(1, "FORMS"); public static readonly AuthenticationMethod WINDOWSAUTHENTICATION = new AuthenticationMethod(2, "WINDOWS"); public static readonly AuthenticationMethod SINGLESIGNON = new AuthenticationMethod(3, "SSN"); private AuthenticationMethod(int value, string name) { this.name = name; this.value = value; } public override string ToString() { return name; } }</code>
ToString()
明示的なタイプ変換を有効にするために、クラスにマッピングするための静的メンバーを追加できます:
クラスのコンストラクターの辞書に入力してください:
最後に、ユーザー定義のタイプ変換演算子を追加します:
<code class="language-csharp">private static readonly Dictionary<string, AuthenticationMethod> instance = new Dictionary<string, AuthenticationMethod>();</code>これにより、文字列を明示的に
インスタンスに変換して、タイプ変換プロセスをより直接的にすることができます。
以上がタイプセーフ列挙と明示的なタイプ変換を使用して、列挙の文字列表現を改善するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。