Получить типы переменных используемых в классе - C#
Формулировка задачи:
Здравствуйте.
Вопрос такой: есть класс Class1, в нем объявлены переменные разных типов с модификаторами доступа private и pablic.
Нужно узнать сколько в классе переменных private и узнать их тип.
или так:
и
но в массив types не возвращаются типы из Class1 ((
и можно ли к переменой из класса обращаться не по имени (Class1.data) а по индексу, как в массиве ?
спасибо.
Листинг программы
- Type [] types = new Type [2];
- Type typeofclass = typeof(Class1);
- types[0] = test.GetType();
- System.Reflection.ConstructorInfo ci =typeofclass.GetConstructor(types);
Листинг программы
- System.Reflection.ConstructorInfo ci = typeofclass.GetConstructor(types);
- BindingFlags.Public, null,
- CallingConventions.HasThis, types, null);
Листинг программы
- foreach (Type val in types)
- MessageBox.Show(val.ToString());
Решение задачи: «Получить типы переменных используемых в классе»
textual
Листинг программы
- using System;
- using System.Reflection;
- public class FieldInfoClass
- {
- public int myField1 = 0;
- protected string myField2 = null;
- private bool myField3 = false;
- public static void Main()
- {
- FieldInfo[] myFieldInfo;
- Type myType = typeof(FieldInfoClass);
- // 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("\nName : {0}", myFieldInfo[i].Name);
- Console.WriteLine("Declaring Type : {0}", myFieldInfo[i].DeclaringType);
- Console.WriteLine("IsPublic : {0}", myFieldInfo[i].IsPublic);
- Console.WriteLine("MemberType : {0}", myFieldInfo[i].MemberType);
- Console.WriteLine("FieldType : {0}", myFieldInfo[i].FieldType);
- Console.WriteLine("IsFamily : {0}", myFieldInfo[i].IsFamily);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д