C#樹林– category –
プログラミング言語のC#を勉強したときのノートです。
-
C#樹林
[C#] Splitting Strings by a Specified Regex Pattern (Regex.Split)
The standard String.Split method can only split text by fixed characters like commas or spaces. However, using Regex.Split allows you to convert strings into arrays using flexible rules, such as "splitting where a number appears" or "spl... -
C#樹林
【C#】文字列を指定した正規表現パターンで分割する (Regex.Split)
String.Split メソッドは「カンマ」や「空白」といった固定の文字でしか分割できませんが、Regex.Split を使用すると、「数字が現れた場所で区切る」「カンマまたはセミコロンまたは空白で区切る」といった柔軟なルールで文字列を分割配列に変換できます。... -
C#樹林
【C#】正規表現を使った高度な置換処理 (置換パターンとMatchEvaluator)
String.Replace メソッドは単純な文字列の置き換えしかできませんが、Regex.Replace を使用すると、「パターンに一致した部分の書き換え」や「プログラムによる計算結果への置き換え」といった高度な処理が可能になります。 ここでは、「置換パターン($1,... -
C#樹林
[C#] Retrieving “All” Occurrences Matching a Pattern in a String (Regex.Matches)
While Regex.Match retrieves only the "first" match, the Regex.Matches method allows you to retrieve all matching occurrences within a string as a collection (list). This method is essential when you want to extract multiple pieces of dat... -
C#樹林
【C#】文字列の中からパターンに一致する箇所を「すべて」取得する (Regex.Matches)
Regex.Match が「最初の1つ」だけを取得するのに対し、Regex.Matches メソッドを使用すると、文字列内に含まれるすべての一致箇所をコレクション(リスト)として取得できます。 ログファイルからのデータ抽出や、文章内のタグ解析など、複数のデータを取... -
C#樹林
[C#] Extracting a Single Match from a String Using Regex Pattern (Regex.Match)
To find a specific pattern (such as an order number, ID, or amount) within a string and retrieve only the first occurrence, use the Regex.Match method. Also, by using the "Grouping ()" feature of regular expressions, you can extract just... -
C#樹林
【C#】文字列の中から正規表現パターンに一致する箇所をひとつだけ取り出す (Regex.Match)
文字列の中から、特定のパターン(注文番号、ID、金額など)に合致する部分を探し出し、最初に見つかった1つだけを取得するには Regex.Match メソッドを使用します。 また、正規表現の「グループ化 ()」という機能を使うと、「注文ID全体」ではなく「IDの... -
C#樹林
[C#] Checking if a String Matches a Regular Expression Pattern (Regex.IsMatch)
When checking if data entered by a user is in the correct format (e.g., zip codes, phone numbers, or email addresses), Regular Expressions are a very powerful tool. In C#, you can use the Regex.IsMatch method from the System.Text.Regular... -
C#樹林
【C#】文字列が正規表現パターンに一致するか判定する (Regex.IsMatch)
ユーザーが入力したデータが正しい形式になっているか(例:郵便番号、電話番号、メールアドレスなど)をチェックする際、正規表現(Regular Expressions) は非常に強力なツールです。 C#では System.Text.RegularExpressions 名前空間の Regex.IsMatch ... -
C#樹林
[C#] Defining Custom Attributes and Controlling Behavior with Reflection
In C#, in addition to standard attributes (like [Obsolete] or [Serializable]), developers can define their own attributes (custom attributes). By using this, you can attach unique metadata (marks such as "this item is required" or "autom... -
C#樹林
【C#】独自の属性(Attribute)を定義してリフレクションで動作を制御する方法
C#では、標準で用意されている属性([Obsolete] や [Serializable] など)以外に、開発者が独自の属性(カスタム属性)を定義することができます。 これを利用すると、クラスやプロパティに対して独自のメタデータ(「この項目は必須」「この項目は自動変... -
C#樹林
[C#] Batch Convert All Null String Properties to Empty Strings
When saving data to a database or returning data as an API response, you may want to unify all null string properties to empty strings (""). If there are only a few properties, you can manually write if (str == null) str = "";. However, ... -
C#樹林
【C#】オブジェクト内のnullの文字列プロパティをすべて空文字(“”)に一括変換する
データベースへの保存前や、APIレスポンスとしてデータを返す際、文字列プロパティに含まれる null をすべて空文字列("")に統一したい場合があります。 プロパティが数個であれば手動で if (str == null) str = ""; と書けますが、プロパティ数が多い場... -
C#樹林
[C#] Batch Convert All Object Property Names and Values to a Dictionary
When creating request parameters for an API or outputting logs, you may often want to convert a class or anonymous type object into "property name" and "value" pairs (key-value format). By using Reflection, you can create a generic conve... -
C#樹林
【C#】オブジェクトの全プロパティ名と値をDictionaryに一括変換する
APIへのリクエストパラメータ作成やログ出力などで、クラスや匿名型のオブジェクトを「プロパティ名」と「値」のペア(キーバリュー形式)に変換したい場合があります。 リフレクションを使用することで、型定義に依存しない汎用的な変換メソッドを作成で... -
C#樹林
[C#] How to Get Attributes Attached to a Class
You can use the GetCustomAttributes method of the Type class to retrieve metadata attached to a class definition (such as author information, version, deprecation warnings, or database table names) at runtime. This allows you to search f... -
C#樹林
【C#】クラス自体に付与された属性(Attribute)を取得する
クラス定義そのものに付与されたメタデータ(作成者情報、バージョン、非推奨マーク、DBテーブル名など)をプログラム実行時に取得するには、Type クラスの GetCustomAttributes メソッドを使用します。 これにより、特定の属性が付与されたクラスだけを検... -
C#樹林
[C#] Retrieving Attribute Values Assigned to Enum Members to Change Display Names
When displaying Enums in a UI (such as dropdown lists or labels), you often want to show a user-friendly name (e.g., "Credit Card" in Japanese) instead of the code identifier (e.g., CreditCard). In such cases, the standard approach is to... -
C#樹林
【C#】列挙型(Enum)のメンバーに付与された属性値を取得して表示名を変える
列挙型(Enum)をUI(ドロップダウンリストやラベル)に表示する際、コード上の識別子(例:CreditCard)ではなく、ユーザーに分かりやすい日本語の名称(例:"クレジットカード")を表示したいケースが多々あります。 このような場合、各メンバに [Displa... -
C#樹林
[C#] Getting All Attributes Applied to a Method (GetCustomAttributes)
Using C# Reflection, you can investigate at runtime what Attributes are applied not only to classes but also to methods. This is useful for checking method metadata during debugging or for scenarios where you want to extract and execute ... -
C#樹林
【C#】メソッドに付与されているすべての属性を取得する (GetCustomAttributes)
C#のリフレクション機能を使用すると、クラスだけでなくメソッドに対しても、どのような属性(Attribute)が付与されているかを実行時に調査できます。 これは、デバッグ時にメソッドのメタデータを確認したり、独自のフレームワークで特定の属性が付いた... -
C#樹林
【C#】プロパティに付与された属性の設定値を取得する (GetCustomAttribute)
特定のプロパティに付与された属性(Attribute)が存在するかを確認するだけでなく、その属性に設定された具体的な値(例:画面表示用のラベル名、エラーメッセージ、設定値など)を読み取りたいケースがあります。 C#のリフレクションと拡張メソッド GetC... -
C#樹林
[C#] How to Determine if a Property Has a Specific Attribute (IsDefined Method)
Using the C# "Attribute" feature allows you to add metadata (such as maximum character length, required fields, or database column names) to classes or properties. To check whether these attributes are attached to a specific property at ... -
C#樹林
【C#】プロパティに特定の属性が付与されているか判定する (IsDefinedメソッド)
C#の「属性(Attribute)」機能を使用すると、クラスやプロパティに追加のメタデータ(最大文字数、必須項目、DBのカラム名など)を付与できます。 プログラムの実行時に、特定のプロパティにこれらの属性が付いているかどうかをチェックするには、リフレ... -
C#樹林
[C#] How to Determine Object Type Classifications (Value Type vs Class) at Runtime
In C#, by referencing properties of the Type class, you can determine classifications such as "is it a value type or reference type," "is it an array," or "is it a generic type" at runtime. This information is extremely useful when creat... -
C#樹林
【C#】実行時にオブジェクトが「値型」か「クラス」かなどの型分類を調べる方法
C#では、Type クラスのプロパティを参照することで、その型が「値型なのか参照型なのか」「配列なのか」「ジェネリック型なのか」といった分類を実行時に判定できます。 汎用的な処理を行うライブラリやデバッグ用メソッドを作成する際に、これらの情報は... -
C#樹林
[C#] Dynamically Get Current Class and Method Names with MethodBase
When recording logs or debugging information, you often want to dynamically retrieve "which method of which class is currently executing" within your program. By using MethodBase.GetCurrentMethod(), part of the C# Reflection feature, you... -
C#樹林
【C#】実行中のクラス名とメソッド名をMethodBaseで動的に取得する
ログ出力やデバッグ情報の記録において、「現在どのクラスの、どのメソッドを実行しているか」という情報をプログラム内で動的に取得したい場合があります。 C#のリフレクション機能の一部である MethodBase.GetCurrentMethod() を使用することで、ハード... -
C#樹林
[C#] How to Get All Constructors and Argument Information Defined in a Class
Using the Type.GetConstructors method in C# Reflection allows you to dynamically list the initialization patterns (constructor overloads) of a class. This technique is commonly used by Dependency Injection (DI) containers to automaticall... -
C#樹林
【C#】クラスに定義されているすべてのコンストラクタと引数情報を取得する方法
リフレクションにおける Type.GetConstructors メソッドを使用すると、そのクラスがどのような初期化パターン(コンストラクタのオーバーロード)を持っているかを動的にリストアップできます。 これは、DI(依存性注入)コンテナが最適なコンストラクタを...