Создаем портфолио за 1 месяц
К концу месяца: 4 классных проекта в вашем портфолио! Стоимость: 100$. Подробности...
1448 of 1492 menu
◀ ▶

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

Смотрите также

← →
↑
Мы используем cookie для работы сайта, аналитики и персонализации. Обработка данных происходит согласно Политике конфиденциальности.
принять все настроить отклонить