Получить типы переменных используемых в классе - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте. Вопрос такой: есть класс Class1, в нем объявлены переменные разных типов с модификаторами доступа private и pablic. Нужно узнать сколько в классе переменных private и узнать их тип.
Листинг программы
  1. Type [] types = new Type [2];
  2. Type typeofclass = typeof(Class1);
  3. types[0] = test.GetType();
  4. System.Reflection.ConstructorInfo ci =typeofclass.GetConstructor(types);
или так:
Листинг программы
  1. System.Reflection.ConstructorInfo ci = typeofclass.GetConstructor(types);
  2. BindingFlags.Public, null,
  3. CallingConventions.HasThis, types, null);
и
Листинг программы
  1. foreach (Type val in types)
  2. MessageBox.Show(val.ToString());
но в массив types не возвращаются типы из Class1 (( и можно ли к переменой из класса обращаться не по имени (Class1.data) а по индексу, как в массиве ? спасибо.

Решение задачи: «Получить типы переменных используемых в классе»

textual
Листинг программы
  1. using System;
  2. using System.Reflection;
  3.  
  4. public class FieldInfoClass
  5. {
  6.     public int myField1 = 0;
  7.     protected string myField2 = null;
  8.     private bool myField3 = false;
  9.  
  10.     public static void Main()
  11.     {
  12.         FieldInfo[] myFieldInfo;
  13.         Type myType = typeof(FieldInfoClass);
  14.         // Get the type and fields of FieldInfoClass.
  15.         myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
  16.             | BindingFlags.Public);
  17.         Console.WriteLine("\nThe fields of " +
  18.             "FieldInfoClass are \n");
  19.         // Display the field information of FieldInfoClass.
  20.         for (int i = 0; i < myFieldInfo.Length; i++)
  21.         {
  22.             Console.WriteLine("\nName            : {0}", myFieldInfo[i].Name);
  23.             Console.WriteLine("Declaring Type  : {0}", myFieldInfo[i].DeclaringType);
  24.             Console.WriteLine("IsPublic        : {0}", myFieldInfo[i].IsPublic);
  25.             Console.WriteLine("MemberType      : {0}", myFieldInfo[i].MemberType);
  26.             Console.WriteLine("FieldType       : {0}", myFieldInfo[i].FieldType);
  27.             Console.WriteLine("IsFamily        : {0}", myFieldInfo[i].IsFamily);
  28.         }
  29.     }
  30. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.125 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы