EchoTex_Payroll/Ease.Core/Utility/ObjectUtility.cs

478 lines
18 KiB
C#
Raw Permalink Normal View History

2024-10-14 10:01:49 +06:00
/*
|-------------------------------------------------------------------------------|
| 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>
/// Summary description for ObjectUtility.
/// </summary>
public sealed class ObjectUtility
{
#region Create Instance
/// <summary>
/// Create instance reference of an object for the given class full name
/// </summary>
/// <param name="className">A valid class name</param>
/// <returns>Returns an instance of the class</returns>
public static object CreateInstance(string className)
{
return ObjectUtility.CreateInstance(className, null);
}
/// <summary>
/// Create instance reference of an object for the given class full name
/// </summary>
/// <param name="className">A valid class name</param>
/// <param name="args">Argument with comma separated</param>
/// <returns>Returns an instance of the class</returns>
public static object CreateInstance(string className, object[] args)
{
return ObjectUtility.CreateInstance(className, args, typeof(ObjectUtility));
}
/// <summary>
/// Create instance reference of an object for the given class full name
/// </summary>
/// <param name="className">A valid class name</param>
/// <param name="args"></param>
/// <param name="type">Type of object to be created </param>
/// <returns>Returns an instance of the class</returns>
public static object CreateInstance(string className, object[] args, System.Type type)
{
return ObjectUtility.CreateInstance(className, args, type, true);
}
/// <summary>
/// Create instance reference of an object for the given class full name
/// into the given assembly
/// </summary>
/// <param name="assembly">A valid assembly name and must be located withing the same folder</param>
/// <param name="className">A valid class name</param>
/// <param name="args"></param>
/// <returns>Returns an instance of the class</returns>
public static object CreateInstance(string assembly, string className, object[] args)
{
return ObjectUtility.CreateInstance(assembly, className, args, true);
}
/// <summary>
/// Create instance reference of an object for the given class full name
/// into the given assembly
/// </summary>
/// <param name="assembly">A valid assembly name and must be located withing the same folder</param>
/// <param name="className">A valid class name</param>
/// <param name="args"></param>
/// <param name="throwNullError"></param>
/// <returns>Returns an instance of the class</returns>
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);
}
/// <summary>
/// Create instance reference of an object for the given class full name
/// </summary>
/// <param name="className"></param>
/// <param name="args"></param>
/// <param name="type"></param>
/// <param name="throwNullError"></param>
/// <returns>Returns an instance of the class</returns>
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
/// <summary>
/// Finds out the custom attribute in the value
/// </summary>
/// <param name="Type">Tyep of attribute</param>
/// <param name="value">Instance of an object </param>
/// <returns>Returns the attribute if found otherwise null</returns>
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;
}
/// <summary>
/// Find out the property from the given type, where property can
/// consists of key path.
/// </summary>
/// <param name="type"></param>
/// <param name="property"></param>
/// <returns>Returns PropertyInfo object of the given property</returns>
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
/// <summary>
/// Find out the property from the given type, where property can
/// consists of key path and returns the value of the property
/// </summary>
/// <param name="source"></param>
/// <param name="property"></param>
/// <returns>Returns value of the given property</returns>
public static object GetPropertyValue(object source, string property)
{
return ObjectUtility.GetPropertyValue(source, property, new object[] { });
}
/// <summary>
/// Find out the property from the given type, where property can
/// consists of key path and returns the value of the property
/// </summary>
/// <param name="source"></param>
/// <param name="property"></param>
/// <param name="indexes"></param>
/// <returns>Returns value of the given property</returns>
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
/// <summary>
/// Find out all properties that have set functionality from the given type
/// </summary>
/// <param name="type"></param>
/// <returns>Returns list of PropertyInfo object of the given type</returns>
public static object[] GetProperties(Type type)
{
if (type == null)
return null;
return (object[])type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
}
/// <summary>
/// Find out all properties that have set functionality from the given type
/// </summary>
/// <param name="type"></param>
/// <param name="attributeType"></param>
/// <returns>Returns list of PropertyInfo object of the given type</returns>
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));
}
/// <summary>
/// Find out all properties that have set functionality from the given type
/// </summary>
/// <param name="type"></param>
/// <param name="attributeType"></param>
/// <param name="includeChildProperties"></param>
/// <returns>Returns list of PropertyInfo object of the given type</returns>
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
/// <summary>
/// Find the property value from the given object
/// </summary>
/// <param name="source"></param>
/// <param name="property"></param>
/// <returns></returns>
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.
/// </summary>
/// <param name="source"></param>
/// <param name="property"></param>
/// <param name="value"></param>
/// <param name="index"></param>
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);
}
/// <summary>
/// Find out the property from the given object, where property can
/// consists of key path and set the value for that property.
/// </summary>
/// <param name="source"></param>
/// <param name="propertyName"></param>
/// <param name="value"></param>
/// <param name="index"></param>
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
}