Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TR GetObjectProperty<T, TR>(T p, Expression<Func<T, TR>> action)
- {
- var expression = (MemberExpression)action.Body;
- var name = expression.Member.Name;
- try
- {
- return (TR)TypeDescriptor.GetProperties(p).Find(name, true).GetValue(p);
- }
- catch
- {
- return (TR)GetDefault(TypeDescriptor.GetProperties(p).Find(name, true).PropertyType);
- }
- }
- public object GetDefault(Type t)
- {
- return this.GetType().GetMethod("GetDefaultGeneric").MakeGenericMethod(t).Invoke(this, null);
- }
- public T GetDefaultGeneric<T>()
- {
- if (typeof(T).IsValueType)
- return default(T);
- else
- return Activator.CreateInstance<T>();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement