Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. public class person()
  2. {
  3. public string name {get; set;}
  4. public int age {get; set;}
  5. }
  6.  
  7. public void Check<T>(Expression<Func<T>> expr)
  8. {
  9. var body = ((MemberExpression) expr.Body);
  10. string name = body.Member.Name;
  11. }
  12.  
  13. Check(() => person1.age);
  14.  
  15. foreach( var property in person)
  16. {
  17. Check(() => property);
  18. }
  19.  
  20. Type type = person1.GetType();
  21. PropertyInfo[] properties = type.GetProperties();
  22.  
  23. foreach (PropertyInfo property in properties)
  24. {
  25. Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(person1, null));
  26. }
  27.  
  28. using System.Reflection;
  29.  
  30. // get all public static properties of MyClass type
  31. PropertyInfo[] propertyInfos;
  32. propertyInfos = typeof(person).GetProperties(BindingFlags.Public |
  33. BindingFlags.Static);
  34. // sort properties by name
  35. Array.Sort(propertyInfos,
  36. delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)
  37. { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });
  38.  
  39.  
  40. foreach (PropertyInfo propertyInfo in propertyInfos)
  41. {
  42. Console.WriteLine(propertyInfo.Name);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement