| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
727 バイト追加 、 2022年8月5日 (金) 14:06
==[[Tips]]==
===属性の指定と読み込み===
[https://docs.microsoft.com/ja-jp/dotnet/csharp/programming-guide/concepts/attributes/accessing-attributes-by-using-reflection リフレクションを使用した属性へのアクセス]
 
* 属性定義
<pre>
[System.AttributeUsage(System.AttributeTargets.Property)]
public class PrimaryKeyAttribute : System.Attribute
{
}
</pre>
 
*読み出し(Hogeクラスのプロパティに属性をつけているとする)
<pre>
var type = typeof(Hoge);
var properties = type.GetProperties();
foreach(var property in properties)
{
foreach(var attr in property.GetCustomAttributes(true))
{
Console.WriteLine($"[{attr}]");
}
Console.WriteLine($"{property.Name}");
}
</pre>
 
 
===Docコメント===
https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/xmldoc/recommended-tags

案内メニュー