|
using System;
using System.Reflection;
namespace SimpleRulesEngine.Utility
{
/// <summary>
/// compare two objects (Properties & Fileds) of the same type
/// </summary>
/// <typeparam name="T"></typeparam>
public static class ObjectUtility<T>
{
/// <summary>
/// Compares the specified property specified by propertyName in x with the value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="propertyName">Name of the STR property.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static int Compare(T x, string strPropertyName, IComparable value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
int difference = -1;
try
{
object propertyValue = GetPropertyValueAdvanced(x, strPropertyName);
if (null != propertyValue)
{
difference = -value.CompareTo(propertyValue);
}
}
catch
{
}
return difference;
}
/// <summary>
/// Compares the specified property specified by propertyName in x with the value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="propertyName">Name of the STR property.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static int Compare(T x, string strPropertyName, object value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
int difference = 0;
try
{
object propertyValue = GetPropertyValueAdvanced(x, strPropertyName);//property.GetValue(x, null);
if (null != propertyValue)
{
difference = (value.Equals(propertyValue)) ? 0 : -1;
}
}
catch
{
}
return difference;
}
/// <summary>
/// Gets the property value.
/// Property path can be nested property path in the dot format ex. object.prop.prop
/// Supports fetching values from properties of type Dictionary `2.
/// </summary>
/// <param name="objValueSource">The obj value source.</param>
/// <param name="strPropertyPath">The STR property path.</param>
/// <returns>value of the object</returns>
public static object GetPropertyValueAdvanced(object objValueSource, string strPropertyPath)
{
Type objDataType = objValueSource.GetType();
string strTemp = strPropertyPath.Contains(".") ? strPropertyPath.Substring(0, strPropertyPath.IndexOf('.')) : strPropertyPath;
string strIndex = null;
if (strTemp.Contains("["))
{
string[] split = strTemp.Split(new string[] { "[", "]" }, StringSplitOptions.RemoveEmptyEntries);
strTemp = split[0];
strIndex = split[1];
}
PropertyInfo property = objDataType.GetProperty(strTemp);
object tempObj;
if (null == property)
throw new Exception("Property not found on the object!");
if (property.PropertyType.BaseType.Name.Equals("Dictionary`2"))
{
tempObj = property.GetValue(objValueSource, null);
property = tempObj.GetType().GetProperty("Item");
object tempObj2 = null;
try
{
tempObj2 = property.GetValue(tempObj, new object[] { strIndex });
}
catch
{
throw new Exception("Item not present in the dictionary!");
}
tempObj = tempObj2;
}
else
{
tempObj = property.GetValue(objValueSource, null);
}
while (tempObj != null && strPropertyPath.Contains("."))
{
Type type = tempObj.GetType();//property.PropertyType;
strPropertyPath = strPropertyPath.Substring(strPropertyPath.IndexOf(".") + 1);
strTemp = strPropertyPath.Contains(".") ? strPropertyPath.Substring(0, strPropertyPath.IndexOf(".")) : strPropertyPath;
if (strTemp.Contains("["))
{
string[] split = strTemp.Split(new string[] { "[", "]" }, StringSplitOptions.RemoveEmptyEntries);
strTemp = split[0];
strIndex = split[1];
}
property = type.GetProperty(strTemp);
if (null == property)
throw new Exception("Property not found on the object!");
if (property.PropertyType.Name.Equals("Dictionary`2"))
{
tempObj = property.GetValue(tempObj, null);
property = tempObj.GetType().GetProperty("Item");
object tempObj2 = null;
try
{
tempObj2 = property.GetValue(tempObj, new object[] { strIndex });
}
catch
{
throw new Exception("Item not present in the dictionary!");
}
tempObj = tempObj2;
}
else
{
tempObj = property.GetValue(tempObj, null);
}
}
return tempObj;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.