Advertisement
stefanpu

BindingFlagsOrder

Feb 25th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 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.                 if (tomProperties[0].Name!="Age" | tomProperties[1].Name!="Name")
  23.                 {
  24.                     Console.WriteLine("Order changed.");
  25.  
  26.                     foreach (var property in tomProperties)
  27.                     {
  28.                         Console.WriteLine(property.Name + ": " + property.GetValue(tom));
  29.                     }
  30.                     break;
  31.                 }
  32.                
  33.             }
  34.  
  35.             foreach (var property in tomProperties)
  36.             {
  37.                 Console.WriteLine(property.Name + ": " + property.GetValue(tom));
  38.             }
  39.         }
  40.     }
  41.  
  42.     class Cat
  43.     {
  44.         public int Age { get; set; }
  45.         public string Name { get; set; }
  46.         public string Breed { get; set; }        
  47.  
  48.         //The following properties won`t be included in the search -
  49.         // one is static and the other is private.
  50.         public static int FeetCount { get; set; }
  51.         private int Weight { get; set; }
  52.  
  53.         public Cat(int age, string name, string breed)
  54.         {
  55.             Age = age;
  56.             Name = name;
  57.             Breed = breed;
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement