Advertisement
stefanpu

BindingFlagsOrder

Feb 25th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4.  
  5. namespace BindingFlagsExamples
  6. {
  7.     class BindingFlagsOrder
  8.     {
  9.        
  10.         static void Main(string[] args)
  11.         {
  12.             Cat tom = new Cat(1, "Tom", "mekuun");
  13.             Type tomType = typeof(Cat);
  14.             PropertyInfo[] tomProperties =
  15.                 tomType.GetProperties(BindingFlags.Public | BindingFlags.Instance);            
  16.  
  17.             for (int i = 0; i < 10000000; i++)
  18.             {
  19.                 tomProperties =
  20.                 tomType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
  21.                
  22.                 //No need to check the third property. If the first two are in
  23.                 //positions 0 and 1, then the last is at the only remaining position.
  24.                 if (tomProperties[0].Name!="Age" | tomProperties[1].Name!="Name")
  25.                 {
  26.                     Console.WriteLine("Order changed.");
  27.  
  28.                     foreach (var property in tomProperties)
  29.                     {
  30.                         Console.WriteLine(property.Name + ": " + property.GetValue(tom));
  31.                     }
  32.                     break;
  33.                 }
  34.                
  35.             }
  36.  
  37.             foreach (var property in tomProperties)
  38.             {
  39.                 Console.WriteLine(property.Name + ": " + property.GetValue(tom));
  40.             }
  41.         }
  42.     }
  43.  
  44.     class Cat
  45.     {
  46.         public int Age { get; set; }
  47.         public string Name { get; set; }
  48.         public string Breed { get; set; }        
  49.  
  50.         //The following properties won`t be included in the search -
  51.         // one is static and the other is private.
  52.         public static int FeetCount { get; set; }
  53.         private int Weight { get; set; }
  54.  
  55.         public Cat(int age, string name, string breed)
  56.         {
  57.             Age = age;
  58.             Name = name;
  59.             Breed = breed;
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement