Created
January 27, 2015 04:07
-
-
Save BernardoGO/499d3a2f734b7f12f56a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Reflection; | |
namespace Reflect | |
{ | |
///by: Bernardo Godinho <bernardo.godinho.oliveira@gmail.com> | |
class ReflectionValues | |
{ | |
public static object GetPropValue(object src, string propName) | |
{ | |
return src.GetType().GetProperty(propName).GetValue(src, null); | |
} | |
public static int GetPropCount(object src) | |
{ | |
return src.GetType().GetProperties().Count(); | |
} | |
public static PropertyInfo[] GetProps(object src) | |
{ | |
PropertyInfo[] props = src.GetType().GetProperties(); | |
return props; | |
} | |
public static void RunMethod(object src, string propName, object[] parms) | |
{ | |
try | |
{ | |
src.GetType().GetMethod(propName).Invoke(src, parms); | |
} | |
catch { } | |
} | |
public static void SetPropValue(object src, string propName, object value) | |
{ | |
if (value.GetType() != typeof(System.DBNull)) | |
{ | |
Type pType = null; | |
try | |
{ | |
pType = src.GetType().GetProperty(propName).PropertyType; | |
} | |
catch | |
{ | |
propName = propName[0].ToString().ToUpper() + propName.Substring(1, propName.Length - 1); | |
pType = src.GetType().GetProperty(propName).PropertyType; | |
} | |
if (pType == typeof(string)) | |
{ | |
src.GetType().GetProperty(propName).SetValue(src, value.ToString(), null); | |
} | |
else if (pType == typeof(bool)) | |
{ | |
src.GetType().GetProperty(propName).SetValue(src, Convert.ToBoolean(TryConvert.ToInt32(value, 0)), null); | |
} | |
else if (pType == typeof(Int32)) | |
{ | |
src.GetType().GetProperty(propName).SetValue(src, TryConvert.ToInt32(value,0), null); | |
} | |
else if (pType.BaseType == typeof(Enum)) | |
{ | |
src.GetType().GetProperty(propName).SetValue(src, TryConvert.ToInt32(value, 0), null); | |
} | |
else if (pType == typeof(short)) | |
{ | |
src.GetType().GetProperty(propName).SetValue(src, TryConvert.ToInt32(value, 0), null); | |
} | |
else if (pType == typeof(Int16)) | |
{ | |
src.GetType().GetProperty(propName).SetValue(src, TryConvert.ToInt16(value, 0), null); | |
} | |
else if (pType == typeof(Int64)) | |
{ | |
src.GetType().GetProperty(propName).SetValue(src, TryConvert.ToInt64(value, 0), null); | |
} | |
else if (pType == typeof(decimal)) | |
{ | |
src.GetType().GetProperty(propName).SetValue(src, TryConvert.ToDecimal(value, 0), null); | |
} | |
else if (pType == typeof(DateTime)) | |
{ | |
string valS = value.ToString();//.Substring(0, 10); | |
src.GetType().GetProperty(propName).SetValue(src, TryConvert.ToDateTime(valS, new DateTime()), null); | |
} | |
else | |
{ | |
} | |
} | |
} | |
public static object GetEnumValue(object src) | |
{ | |
return src.GetType().GetEnumValues(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment