Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Reflection;
- namespace BindingFlagsExamples
- {
- class BindingFlagsOrder
- {
- static void Main(string[] args)
- {
- Cat tom = new Cat(1, "Tom", "mekuun");
- Type tomType = typeof(Cat);
- PropertyInfo[] tomProperties =
- tomType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
- for (int i = 0; i < 10000000; i++)
- {
- tomProperties =
- tomType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
- //No need to check the third property. If the first two are in
- //positions 0 and 1, then the last is at the only remaining position.
- if (tomProperties[0].Name!="Age" | tomProperties[1].Name!="Name")
- {
- Console.WriteLine("Order changed.");
- foreach (var property in tomProperties)
- {
- Console.WriteLine(property.Name + ": " + property.GetValue(tom));
- }
- break;
- }
- }
- foreach (var property in tomProperties)
- {
- Console.WriteLine(property.Name + ": " + property.GetValue(tom));
- }
- }
- }
- class Cat
- {
- public int Age { get; set; }
- public string Name { get; set; }
- public string Breed { get; set; }
- //The following properties won`t be included in the search -
- // one is static and the other is private.
- public static int FeetCount { get; set; }
- private int Weight { get; set; }
- public Cat(int age, string name, string breed)
- {
- Age = age;
- Name = name;
- Breed = breed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement