Свойство 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
Смотрите также
-
GetValue,
чтение значения -
ReturnType,
тип результата метода -
ParameterInfo,
параметр метода -
PropertyInfo,
обзор