extensions/BasicExtensions.cs
2017-04-07 12:36:42 +02:00

23 lines
775 B
C#

using System.Reflection;
namespace Chrosey.Extensions
{
public static class BasicExtensions
{
public static bool HasProperty(this object me, string propertyName)
{
return me.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) != null;
}
public static object GetPropertyValue(this object me, string propertyName)
{
if (!me.HasProperty(propertyName)) throw new NotFoundException("Property not found " + propertyName);
return me
.GetType()
.GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
.GetValue(me, null);
}
}
}