(精华)2020年6月26日 C#类库 ExpandoObject(扩展方法)
【摘要】
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System....
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
namespace Core.Util
{
public static partial class Extention
{
/// <summary>
/// 添加属性
/// </summary>
/// <param name="expandoObj">动态对象</param>
/// <param name="propertyName">属性名</param>
/// <param name="value">属性值</param>
public static void AddProperty(this ExpandoObject expandoObj,string propertyName,object value)
{
var obj = (IDictionary<string, object>)expandoObj;
if (obj.ContainsKey(propertyName))
throw new Exception("已存在该属性!");
else
obj.Add(propertyName, value);
}
/// <summary>
/// 设置属性
/// </summary>
/// <param name="expandoObj">动态对象</param>
/// <param name="propertyName">属性名</param>
/// <param name="value">属性值</param>
public static void SetProperty(this ExpandoObject expandoObj, string propertyName, object value)
{
var obj = (IDictionary<string, object>)expandoObj;
if (!obj.ContainsKey(propertyName))
obj.Add(propertyName, value);
else
obj[propertyName] = value;
}
/// <summary>
/// 获取属性
/// </summary>
/// <param name="expandoObj">动态对象</param>
/// <param name="propertyName">属性名</param>
/// <returns></returns>
public static object GetProperty(this ExpandoObject expandoObj, string propertyName)
{
var obj = (IDictionary<string, object>)expandoObj;
if (!obj.ContainsKey(propertyName))
throw new Exception("不存在该属性!");
else
return obj[propertyName];
}
/// <summary>
/// 获取所有属性
/// </summary>
/// <param name="expandoObj">动态对象</param>
/// <returns></returns>
public static List<string> GetProperties(this ExpandoObject expandoObj)
{
var obj = (IDictionary<string, object>)expandoObj;
return obj.Keys.CastToList<string>();
}
/// <summary>
/// 删除属性
/// </summary>
/// <param name="expandoObj">动态对象</param>
/// <param name="propertyName">属性名</param>
public static void RemoveProperty(this ExpandoObject expandoObj, string propertyName)
{
var obj = (IDictionary<string, object>)expandoObj;
if (!obj.ContainsKey(propertyName))
throw new Exception("不存在该属性!");
else
obj.Remove(propertyName);
}
/// <summary>
/// 将动态属性对象ExpandoObject列表转为DataTable
/// </summary>
/// <param name="dataList">数据源</param>
/// <returns></returns>
public static DataTable ToDataTable(this IEnumerable<ExpandoObject> dataList)
{
DataTable dt = new DataTable();
if (dataList.IsNullOrEmpty())
return null;
else if (dataList.Count() == 0)
return dt;
else
{
var aEntity = dataList.FirstOrDefault();
var properties = aEntity.GetProperties();
properties.ForEach(aProperty =>
{
dt.Columns.Add(aProperty);
});
dataList.ForEach((aData,index) =>
{
dt.Rows.Add(dt.NewRow());
properties.ForEach(aProperty =>
{
dt.Rows[index][aProperty] = aData.GetProperty(aProperty);
});
});
}
return dt;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/106971827
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)