code_junkie

How do I get the name of a property from a property in C# (2.0)

Nov 13th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. class Whotsit
  2. {
  3. private string testProp = "thingy";
  4.  
  5. public string TestProp
  6. {
  7. get { return testProp; }
  8. set { testProp = value; }
  9. }
  10.  
  11. }
  12.  
  13. ...
  14.  
  15. Whotsit whotsit = new Whotsit();
  16. string value = GetName(whotsit.TestProp); //precise syntax up for grabs..
  17.  
  18. /// <summary>
  19. /// Populates an object from a datatable where the rows have columns called NameField and ValueField.
  20. /// If the property with the 'name' exists, and is not read-only, it is populated from the
  21. /// valueField. Any other columns in the dataTable are ignored. If there is no property called
  22. /// nameField it is ignored. Any properties of the object not found in the data table retain their
  23. /// original values.
  24. /// </summary>
  25. /// <typeparam name="T">Type of the object to be populated.</typeparam>
  26. /// <param name="toBePopulated">The object to be populated</param>
  27. /// <param name="dataTable">'name, 'value' Data table to populate the object from.</param>
  28. /// <param name="nameField">Field name of the 'name' field'.</param>
  29. /// <param name="valueField">Field name of the 'value' field.</param>
  30. /// <param name="options">Setting to control conversions - e.g. nulls as empty strings.</param>
  31.  
  32. public static void PopulateFromNameValueDataTable<T>
  33. (T toBePopulated, System.Data.DataTable dataTable, string nameField, string valueField, PopulateOptions options)
  34. {
  35. Type type = typeof(T);
  36. bool nullStringsAsEmptyString = options == PopulateOptions.NullStringsAsEmptyString;
  37.  
  38. foreach (DataRow dataRow in dataTable.Rows)
  39. {
  40. string name = dataRow[nameField].ToString();
  41. System.Reflection.PropertyInfo property = type.GetProperty(name);
  42. object value = dataRow[valueField];
  43.  
  44. if (property != null)
  45. {
  46. Type propertyType = property.PropertyType;
  47. if (nullStringsAsEmptyString && (propertyType == typeof(String)))
  48. {
  49. value = TypeHelper.EmptyStringIfNull(value);
  50. }
  51. else
  52. {
  53. value = TypeHelper.DefaultIfNull(value, propertyType);
  54. }
  55.  
  56. property.SetValue(toBePopulated, System.Convert.ChangeType(value, propertyType), null);
  57. }
  58. }
  59. }
  60.  
  61. // I wish...
  62. MemberInfo member = infoof(whotsit.TestProp);
  63.  
  64. Expression<Func<string>> = () => whotsit.TestProp;
  65.  
  66. class Program
  67. {
  68. static void Main()
  69. {
  70. string propertyName = GetName(() => AppDomain.CurrentDomain);
  71. Console.WriteLine(propertyName); // prints "CurrentDomain"
  72. Console.ReadLine();
  73. }
  74.  
  75. public static string GetName(Expression<Func<object>> property)
  76. {
  77. return property.Body.ToString().Split('.').Last();
  78. }
  79. }
  80.  
  81. Type t = whotsit.GetType();
  82. PropertyInfo[] pis = t.GetProperties();
  83.  
  84. class TestClass
  85. {
  86. private string _field;
  87.  
  88. public string MyProperty
  89. {
  90. get { return _field; }
  91. }
  92. }
  93. class Program
  94. {
  95. static void Main(string[] args)
  96. {
  97. TestClass test = new TestClass();
  98. PropertyInfo[] info = test.GetType().GetProperties();
  99. foreach(PropertyInfo i in info)
  100. Console.WriteLine(i.Name);
  101. Console.Read();
  102. }
  103. }
  104.  
  105. Whotsit w = new Whotsit();
  106. XmlSerializer xs = new XmlSerializer(w.TestProp.GetType());
  107. TextWriter sw = new StreamWriter(@"c:TestProp.xml");
  108. xs.Serialize(sw, w.TestProp);
  109. sw.Close();
Add Comment
Please, Sign In to add comment