PropertyInfo
Объект получают из
GetProperties
или GetProperty.
Чтение и запись -
GetValue
и
SetValue.
Синтаксис
PropertyInfo prop = type.GetProperty("Count");
Пример
Давайте прочитать имя свойства
Count:
using System;
using System.Reflection;
class Gate
{
public int Count { get; set; }
}
class Program
{
static void Main()
{
PropertyInfo prop = typeof(Gate).GetProperty("Count");
string res = prop.Name;
Console.WriteLine(res);
}
}
Результат выполнения кода:
"Count"
Пример
Давайте прочитать значение через
GetValue:
using System;
using System.Reflection;
class Gate
{
public int Count { get; set; } = 12345;
}
class Program
{
static void Main()
{
Gate gate = new Gate();
PropertyInfo prop = typeof(Gate).GetProperty("Count");
int res = (int)prop.GetValue(gate);
Console.WriteLine(res);
}
}
Результат выполнения кода:
12345
Пример
Давайте узнать тип свойства:
using System;
using System.Reflection;
class Gate
{
public int Count { get; set; }
}
class Program
{
static void Main()
{
PropertyInfo prop = typeof(Gate).GetProperty("Count");
bool res = prop.PropertyType == typeof(int);
Console.WriteLine(res);
}
}
Результат выполнения кода:
True
Смотрите также
-
GetValue,
чтение -
SetValue,
запись -
GetProperties,
список свойств -
FieldInfo,
поле