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

Метод GetValue

Первый аргумент - экземпляр, для которого читают свойство. Запись - SetValue.

Синтаксис

object value = prop.GetValue(target);

Пример

Давайте прочитать Count:

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() { Gate gate = new Gate { Count = 12345 }; PropertyInfo prop = typeof(Gate).GetProperty("Count"); int res = (int)prop.GetValue(gate); Console.WriteLine(res); } }

Результат выполнения кода:

12345

Пример

Давайте прочитать строковое свойство:

using System; using System.Reflection; class Gate { public string Name { get; set; } = "abcde"; } class Program { static void Main() { Gate gate = new Gate(); PropertyInfo prop = typeof(Gate).GetProperty("Name"); string res = (string)prop.GetValue(gate); Console.WriteLine(res); } }

Результат выполнения кода:

"abcde"

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

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