Получить список свойств класса - C#
Формулировка задачи:
Добрый вечер,
Необходимо получить список свойств класса и записать его в массив/список.
Однако в результате я получаю не список свойств в виде <OwnerSurname>k_BackingField вместо OwnerSurname. Как это исправить?
Заранее спасибо!
Листинг программы
- using System;
- using System.Reflection;
- namespace ConsoleApplication30
- {
- public class OwnerType
- {
- public string OwnerSurname { get; set; }
- public string OwnerFirstName { get; set; }
- public string OwnerStreet { get; set; }
- public string OwnerBlock { get; set; }
- public string OwnerCorpus { get; set; }
- public string OwnerBuild { get; set; }
- public string OwnerFlat { get; set; }
- public string OwnerPhone { get; set; }
- }
- public class FieldInfoClass
- {
- public static void Main()
- {
- FieldInfo[] myFieldInfo;
- Type myType = typeof(OwnerType);
- // Get the type and fields of FieldInfoClass.
- myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
- | BindingFlags.Public);
- Console.WriteLine("\nThe fields of " +
- "FieldInfoClass are \n");
- // Display the field information of FieldInfoClass.
- for (int i = 0; i < myFieldInfo.Length; i++)
- {
- Console.WriteLine( myFieldInfo[i].Name);
- }
- Console.ReadKey();
- }
- }
- }
Решение задачи: «Получить список свойств класса»
textual
Листинг программы
- PropertyInfo[] myPropertyInfo;
- Type myType = typeof(OwnerType);
- // Get the type and fields of FieldInfoClass.
- myPropertyInfo = myType.GetProperties();
- Console.WriteLine("\nThe property of " +
- "FieldInfoClass are \n");
- for (int i = 0; i < myPropertyInfo.Length; i++)
- Console.WriteLine(myPropertyInfo[i].Name);
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д