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

Свойство PropertyType

Тип сравнивают с typeof. Для свойства Count типа int результат равен typeof(int).

Синтаксис

Type type = prop.PropertyType;

Пример

Давайте сравнить тип Count с int:

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

Пример

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

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.PropertyType.Name; Console.WriteLine(res); } }

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

"Int32"

Пример

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

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

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

True

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

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