Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Whotsit
- {
- private string testProp = "thingy";
- public string TestProp
- {
- get { return testProp; }
- set { testProp = value; }
- }
- }
- ...
- Whotsit whotsit = new Whotsit();
- string value = GetName(whotsit.TestProp); //precise syntax up for grabs..
- /// <summary>
- /// Populates an object from a datatable where the rows have columns called NameField and ValueField.
- /// If the property with the 'name' exists, and is not read-only, it is populated from the
- /// valueField. Any other columns in the dataTable are ignored. If there is no property called
- /// nameField it is ignored. Any properties of the object not found in the data table retain their
- /// original values.
- /// </summary>
- /// <typeparam name="T">Type of the object to be populated.</typeparam>
- /// <param name="toBePopulated">The object to be populated</param>
- /// <param name="dataTable">'name, 'value' Data table to populate the object from.</param>
- /// <param name="nameField">Field name of the 'name' field'.</param>
- /// <param name="valueField">Field name of the 'value' field.</param>
- /// <param name="options">Setting to control conversions - e.g. nulls as empty strings.</param>
- public static void PopulateFromNameValueDataTable<T>
- (T toBePopulated, System.Data.DataTable dataTable, string nameField, string valueField, PopulateOptions options)
- {
- Type type = typeof(T);
- bool nullStringsAsEmptyString = options == PopulateOptions.NullStringsAsEmptyString;
- foreach (DataRow dataRow in dataTable.Rows)
- {
- string name = dataRow[nameField].ToString();
- System.Reflection.PropertyInfo property = type.GetProperty(name);
- object value = dataRow[valueField];
- if (property != null)
- {
- Type propertyType = property.PropertyType;
- if (nullStringsAsEmptyString && (propertyType == typeof(String)))
- {
- value = TypeHelper.EmptyStringIfNull(value);
- }
- else
- {
- value = TypeHelper.DefaultIfNull(value, propertyType);
- }
- property.SetValue(toBePopulated, System.Convert.ChangeType(value, propertyType), null);
- }
- }
- }
- // I wish...
- MemberInfo member = infoof(whotsit.TestProp);
- Expression<Func<string>> = () => whotsit.TestProp;
- class Program
- {
- static void Main()
- {
- string propertyName = GetName(() => AppDomain.CurrentDomain);
- Console.WriteLine(propertyName); // prints "CurrentDomain"
- Console.ReadLine();
- }
- public static string GetName(Expression<Func<object>> property)
- {
- return property.Body.ToString().Split('.').Last();
- }
- }
- Type t = whotsit.GetType();
- PropertyInfo[] pis = t.GetProperties();
- class TestClass
- {
- private string _field;
- public string MyProperty
- {
- get { return _field; }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- TestClass test = new TestClass();
- PropertyInfo[] info = test.GetType().GetProperties();
- foreach(PropertyInfo i in info)
- Console.WriteLine(i.Name);
- Console.Read();
- }
- }
- Whotsit w = new Whotsit();
- XmlSerializer xs = new XmlSerializer(w.TestProp.GetType());
- TextWriter sw = new StreamWriter(@"c:TestProp.xml");
- xs.Serialize(sw, w.TestProp);
- sw.Close();
Add Comment
Please, Sign In to add comment