Defining classes
public class Address { public string Steet { get; set; } public string Apartment { get; set; } public string City { get; set; } public string State { get; set; } }
public class Car { public string Make { get; set; } public int Year { get; set; } public string Made { get; set; } }public class Person { public string FirstName { get; set; } public string LastName { get; set; } }public class PersonalInfo { public Car MyCar { get; set; } public Address MyAddress { get; set; } public Person MyInfo { get; set; } }Here we will create an instance of PersonalInfo, from this instance we will retrieve property information.class Program { static void Main(string[] args) { PersonalInfo info = new PersonalInfo(); Type pi = info.GetType(); PropertyInfo[] myPropertyInfo = pi.GetProperties(BindingFlags.Public | BindingFlags.Instance); DisplayPropertyInfo(myPropertyInfo); Console.WriteLine(pi.Name); Console.Read(); } public static void DisplayPropertyInfo(PropertyInfo[] myPropertyInfo) { // Display information for all properties. for (int i = 0; i < myPropertyInfo.Length; i++) { PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i]; Console.WriteLine("The property name is {0}.", myPropInfo.Name); Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType); } } }Output:The property name is MyCar. The property type is ClassLibrary1.Car. The property name is MyAddress. The property type is ClassLibrary1.Address. The property name is MyInfo. The property type is ClassLibrary1.Person. PersonalInfo
Comments
Post a Comment