-
-
Save sonoichi60/e423c9514076b8a87e2dfcf1de7846f8 to your computer and use it in GitHub Desktop.
example FieldInfo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 名前指定でメンバ変数の取得 | |
var field = typeof(TestClass).GetField("Field", BindingFlags.NonPublic | BindingFlags.Instance); | |
// メンバ変数を全取得 | |
var fields = typeof(TestClass).GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); | |
var fieldType = field.FieldType; // 変数タイプ | |
var fieldName = field.Name; // 変数名 | |
var isLiteral = field.IsLiteral; // 定数か | |
var isInitOnly = field.IsInitOnly; // readonlyか | |
// Serializable属性を持つか | |
if (field.IsDefined(typeof(SerializableAttribute), false)) | |
{ | |
// SerializableAttributeの取得 | |
var rangeAttr = field.GetCustomAttribute<SerializableAttribute>(); | |
} | |
// Attributeの全取得 | |
var attrs = field.GetCustomAttributes(); | |
// 値のセット(変数の型が int の場合) | |
var instance = new TestClass(); | |
field.SetValue(instance, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment