/*
|-------------------------------------------------------------------------------|
| Copyright © Computer Ease Limited |
| Address: 1/9 Bloack-A Lalmatia, Dhaka-1207, Bangladesh |
| Email: info@celimited.com, cease@bol-online.com, web: www.celimited.com |
| Unauthorized copy or distribution is strictly prohibited |
| Author: S. M. Russel, Last modified date: 23/07/2012 |
|-------------------------------------------------------------------------------|
*/
using System;
using System.Reflection;
using System.Collections;
using System.Globalization;
namespace Ease.Core.Utility
{
#region Framework: Object utility
///
/// Summary description for ObjectUtility.
///
public sealed class ObjectUtility
{
#region Create Instance
///
/// Create instance reference of an object for the given class full name
///
/// A valid class name
/// Returns an instance of the class
public static object CreateInstance(string className)
{
return ObjectUtility.CreateInstance(className, null);
}
///
/// Create instance reference of an object for the given class full name
///
/// A valid class name
/// Argument with comma separated
/// Returns an instance of the class
public static object CreateInstance(string className, object[] args)
{
return ObjectUtility.CreateInstance(className, args, typeof(ObjectUtility));
}
///
/// Create instance reference of an object for the given class full name
///
/// A valid class name
///
/// Type of object to be created
/// Returns an instance of the class
public static object CreateInstance(string className, object[] args, System.Type type)
{
return ObjectUtility.CreateInstance(className, args, type, true);
}
///
/// Create instance reference of an object for the given class full name
/// into the given assembly
///
/// A valid assembly name and must be located withing the same folder
/// A valid class name
///
/// Returns an instance of the class
public static object CreateInstance(string assembly, string className, object[] args)
{
return ObjectUtility.CreateInstance(assembly, className, args, true);
}
///
/// Create instance reference of an object for the given class full name
/// into the given assembly
///
/// A valid assembly name and must be located withing the same folder
/// A valid class name
///
///
/// Returns an instance of the class
public static object CreateInstance(string assembly, string className, object[] args, bool throwNullError)
{
Type type = null;
Assembly asm = null;
try
{
asm = Assembly.Load(assembly);
}
catch (Exception ex)
{
throw new ArgumentException(
string.Format("Failed to load the assembly '{0}' due to following message:\n{1}", assembly,
ex.Message), "assembly", ex);
}
try
{
type = asm.GetType(className, true);
}
catch (Exception ex)
{
throw new ArgumentException(
string.Format(CultureInfo.CurrentCulture, "Class {0} does not exists in assembly {1}", className,
asm.FullName), "className", ex);
}
return ObjectUtility.CreateInstance(className, args, type, throwNullError);
}
///
/// Create instance reference of an object for the given class full name
///
///
///
///
///
/// Returns an instance of the class
public static object CreateInstance(string className, object[] args, System.Type type, bool throwNullError)
{
if (type == null)
type = typeof(ObjectUtility);
object obj = null;
if (args == null)
obj = type.Assembly.CreateInstance(className);
else
obj = type.Assembly.CreateInstance(className, false,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, args, null, null);
if (obj == null && throwNullError)
throw new ArgumentException(string.Format("Class {0} does not exists in assembly {1}", className,
type.Assembly.FullName));
return obj;
}
#endregion
#region Get Custome Attribute
///
/// Finds out the custom attribute in the value
///
/// Tyep of attribute
/// Instance of an object
/// Returns the attribute if found otherwise null
public static object GetCustomAttribute(Type type, object value)
{
if (type == null || value == null)
return null;
object[] attrs = null;
Type valueType = value.GetType();
if (valueType.IsEnum)
{
FieldInfo field = valueType.GetField(value.ToString());
if (field == null)
return null;
attrs = field.GetCustomAttributes(type, true);
}
else
{
if (value is PropertyInfo)
attrs = ((PropertyInfo)value).GetCustomAttributes(type, true);
else
attrs = valueType.GetCustomAttributes(type, true);
}
if (attrs != null && attrs.Length > 0)
return attrs[0];
return null;
}
///
/// Find out the property from the given type, where property can
/// consists of key path.
///
///
///
/// Returns PropertyInfo object of the given property
public static object GetProperty(Type type, string property)
{
if (type == null || string.IsNullOrEmpty(property))
return null;
if (property.IndexOf(".") == -1)
return type.GetProperty(property,
BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
PropertyInfo propertyInfo = null;
string[] propertyPath = property.Split('.');
foreach (string prop in propertyPath)
{
if (!string.IsNullOrEmpty(prop))
{
propertyInfo = type.GetProperty(prop,
BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
if (propertyInfo == null)
return null;
type = propertyInfo.PropertyType;
}
}
return propertyInfo;
}
#endregion
#region Get Property Value
///
/// Find out the property from the given type, where property can
/// consists of key path and returns the value of the property
///
///
///
/// Returns value of the given property
public static object GetPropertyValue(object source, string property)
{
return ObjectUtility.GetPropertyValue(source, property, new object[] { });
}
///
/// Find out the property from the given type, where property can
/// consists of key path and returns the value of the property
///
///
///
///
/// Returns value of the given property
public static object GetPropertyValue(object source, string property, object[] indexes)
{
if (source == null || string.IsNullOrEmpty(property))
return null;
if (property.IndexOf(".") == -1)
return source.GetType().GetProperty(property).GetValue(source, indexes);
string[] propertyPath = property.Split('.');
foreach (string prop in propertyPath)
{
if (!(string.IsNullOrEmpty(prop)))
{
PropertyInfo propertyInfo = source.GetType().GetProperty(prop);
if (propertyInfo == null)
return null;
source = propertyInfo.GetValue(source, indexes);
}
}
return source;
}
#endregion
#region Get Properties
///
/// Find out all properties that have set functionality from the given type
///
///
/// Returns list of PropertyInfo object of the given type
public static object[] GetProperties(Type type)
{
if (type == null)
return null;
return (object[])type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
}
///
/// Find out all properties that have set functionality from the given type
///
///
///
/// Returns list of PropertyInfo object of the given type
public static object[] GetProperties(Type type, Type attributeType)
{
ArrayList list = new ArrayList();
if (attributeType != null)
{
PropertyInfo[] props = (PropertyInfo[])ObjectUtility.GetProperties(type);
foreach (PropertyInfo prop in props)
{
object[] attributes = prop.GetCustomAttributes(attributeType, true);
if (attributes != null && attributes.Length > 0)
list.Add(prop);
}
}
return (object[])list.ToArray(typeof(PropertyInfo));
}
///
/// Find out all properties that have set functionality from the given type
///
///
///
///
/// Returns list of PropertyInfo object of the given type
public static object[] GetProperties(Type type, Type attributeType, bool includeChildProperties,
Type ignoreType)
{
ArrayList list = new ArrayList();
if (attributeType != null)
{
PropertyInfo[] props = (PropertyInfo[])ObjectUtility.GetProperties(type);
foreach (PropertyInfo prop in props)
{
object[] attributes = prop.GetCustomAttributes(attributeType, true);
if (attributes != null && attributes.Length > 0)
list.Add(prop);
else
{
if (includeChildProperties && prop.PropertyType.BaseType != null
&& prop.PropertyType.BaseType.Equals(type.BaseType))
{
attributes = prop.GetCustomAttributes(ignoreType, true);
if (attributes != null && attributes.Length == 0)
list.AddRange(ObjectUtility.GetProperties(prop.PropertyType, attributeType,
includeChildProperties, ignoreType));
}
}
}
}
return (object[])list.ToArray(typeof(PropertyInfo));
}
#endregion
#region Get Source Property Value
///
/// Find the property value from the given object
///
///
///
///
public static object GetSourcePropertyValue(object source, object property)
{
PropertyInfo prop = property as PropertyInfo;
if (prop == null)
return source;
object src = source;
if (source.GetType().Equals(prop.DeclaringType) == false)
{
src = null;
PropertyInfo[] props = (PropertyInfo[])ObjectUtility.GetProperties(source.GetType());
if (props != null && props.Length > 0)
{
foreach (PropertyInfo sourceProp in props)
{
if (sourceProp.PropertyType.Equals(prop.DeclaringType))
{
src = ObjectUtility.GetPropertyValue(source, sourceProp.Name);
break;
}
else
{
if (sourceProp.PropertyType.BaseType != null &&
sourceProp.PropertyType.BaseType.Equals(prop.DeclaringType.BaseType))
{
object objVal = ObjectUtility.GetPropertyValue(src, sourceProp.Name);
if (objVal != null)
src = ObjectUtility.GetSourcePropertyValue(objVal, prop);
if (src != null)
break;
}
}
}
}
}
return src;
}
#endregion
#region Set Property
/// Find out the property from the given object, where property can
/// consists of key path and set the value for that property.
///
///
///
///
///
public static void SetProperty(object source, object property, object value, object[] index)
{
PropertyInfo pi = property as PropertyInfo;
if (pi == null)
return;
object src = ObjectUtility.GetSourcePropertyValue(source, pi);
if (src != null)
ObjectUtility.SetProperty(src, pi.Name, value, index);
}
///
/// Find out the property from the given object, where property can
/// consists of key path and set the value for that property.
///
///
///
///
///
public static void SetProperty(object source, string propertyName, object value, object[] index)
{
PropertyInfo prop = (PropertyInfo)ObjectUtility.GetProperty(source.GetType(), propertyName);
if (prop == null)
throw new ArgumentNullException(string.Format(CultureInfo.CurrentCulture,
"Class {0} does not have a property named {1}", source.GetType().FullName, propertyName));
try
{
if (prop.PropertyType.IsEnum)
prop.SetValue(source, Enum.Parse(prop.PropertyType, value.ToString(), true), index);
else if (prop.PropertyType.Equals(typeof(bool)))
prop.SetValue(source, Convert.ToBoolean(value, CultureInfo.CurrentCulture), index);
else if (prop.PropertyType.Equals(typeof(DateTime)))
prop.SetValue(source, Convert.ToDateTime(value, CultureInfo.CurrentCulture), index);
else if (prop.PropertyType.Equals(typeof(double)))
prop.SetValue(source, Convert.ToDouble(value, CultureInfo.CurrentCulture), index);
else if (prop.PropertyType.Equals(typeof(decimal)))
prop.SetValue(source, Convert.ToDecimal(value, CultureInfo.CurrentCulture), index);
else if (prop.PropertyType.Equals(typeof(Int16)))
prop.SetValue(source, Convert.ToInt16(value, CultureInfo.CurrentCulture), index);
else if (prop.PropertyType.Equals(typeof(Int32)))
prop.SetValue(source, Convert.ToInt32(value, CultureInfo.CurrentCulture), index);
else if (prop.PropertyType.Equals(typeof(Int64)))
prop.SetValue(source, Convert.ToInt64(value, CultureInfo.CurrentCulture), index);
else if (prop.PropertyType.Equals(typeof(byte)))
prop.SetValue(source, Convert.ToByte(value, CultureInfo.CurrentCulture), index);
else
prop.SetValue(source, value, index);
}
catch
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
"System failed to set value for property named {0} of class {1}", prop.Name,
source.GetType().FullName));
}
}
#endregion
}
#endregion
}