/// <summary>
/// 得到类的属性值列表。以list方式返回
/// </summary>
/// <param name="O"></param>
/// <returns></returns>
public static List<string> getValues(object O)
{
List<string> P = new List<string>();
try
{
PropertyInfo[] propertyInfo = O.GetType().GetProperties();
for (int i = 0; i < propertyInfo.Length; i++)
{
object objectValue = propertyInfo[i].GetGetMethod().Invoke(O, null);
if (objectValue == null)
{
P.Add("''") ;
continue;
}
if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan)
{
P.Add("'" + objectValue.ToString() + "'");
}
else if (objectValue is string)
{
P.Add("'" + objectValue.ToString() + "'");
}
else if(objectValue is Int32 || objectValue is Int64)
{
P.Add(objectValue.ToString());
}
//else if (objectValue is IEnumerable)
//{
// P.Add(getValues((IEnumerable)objectValue));
//}
else
{
P.Add("'" + objectValue.ToString() + "'");
}
}
}
catch (Exception ex)
{
throw ex;
}
return P;
}
/// 得到类的属性值列表。以list方式返回
/// </summary>
/// <param name="O"></param>
/// <returns></returns>
public static List<string> getValues(object O)
{
List<string> P = new List<string>();
try
{
PropertyInfo[] propertyInfo = O.GetType().GetProperties();
for (int i = 0; i < propertyInfo.Length; i++)
{
object objectValue = propertyInfo[i].GetGetMethod().Invoke(O, null);
if (objectValue == null)
{
P.Add("''") ;
continue;
}
if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan)
{
P.Add("'" + objectValue.ToString() + "'");
}
else if (objectValue is string)
{
P.Add("'" + objectValue.ToString() + "'");
}
else if(objectValue is Int32 || objectValue is Int64)
{
P.Add(objectValue.ToString());
}
//else if (objectValue is IEnumerable)
//{
// P.Add(getValues((IEnumerable)objectValue));
//}
else
{
P.Add("'" + objectValue.ToString() + "'");
}
}
}
catch (Exception ex)
{
throw ex;
}
return P;
}
本文介绍了一种通过反射获取对象所有属性值的方法,并将其以字符串列表形式返回。该方法能够处理不同类型的属性值,包括基本类型、字符串、日期等。
1652

被折叠的 条评论
为什么被折叠?



