C# 传入任意类,得到类的属性值,以list返回
来源:互联网 发布:淘宝服装购买合同范本 编辑:程序博客网 时间:2024/06/06 03:48
/// <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;
}
0 0
- C# 传入任意类,得到类的属性值,以list返回
- C#根据字符串得到静态类的属性(PropertyInfo)
- c#传入域名返回对应的IP
- c# 反射得到实体类的字段名称和值,DataTable转List<T>
- jdbc中如何返回一个封装任意类对象的list
- C++ 以传入vector的方式实现返回一个数组
- 传入流得到字符串工具类
- C# SQL带传入、输出参数及返回值的存储过程
- C# 传入引用类型的参数 返回值是否发生变化
- java反射得到类对象,并且得到类的属性
- WP7 Page基类的使用,以及传入任意类型的参数的封装
- c#窗体传入参数及获取返回值
- C# 获取传入对象的所有属性名称
- 得到Activity的返回值
- vbscript数组传入c#写的类库
- C# 获取任意类的实例列表!
- Java通过反射得到其属性的值包含父类属性
- java反射得到类的属性名和属性值并且放入Map中
- 硬碟低級格式化全攻略
- 初学python--python中的self
- 基于交叉熵准则(Cross Entropy Criteria)的神经网络训练
- android widget不更新
- 《设计模式之禅》——建造者模式
- C# 传入任意类,得到类的属性值,以list返回
- 匯 流 排 規 格 ( ISA、PCI、AGP ) 現已不適用
- codevs1369 xth砍树
- 顯示卡名詞解釋(舊資料)
- PB关于日期相关的实用函数
- 解释器模式
- Android学习笔记之PopupMenu弹出菜单
- C# datatable,将datatable表变例,装载成类对象池。
- 窗体上的控件保持相对位置不变