国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 表達式目錄樹的應用詳解

2020-01-24 00:22:28
字體:
來源:轉載
供稿:網友

使用表達式目錄樹實現兩個不同類型的屬性賦值:

public class People{public int Age { get; set; }public string Name { get; set; }public int Id;}public class PeopleCopy{public int Age { get; set; }public string Name { get; set; }public int Id;}public class Class1{private static Dictionary<string, object> _Dic = new Dictionary<string, object>();private static TOut TransExp<TIn, TOut>(TIn tIn) {string key = $"funckey_{typeof(TIn).FullName}_{typeof(TOut).FullName}";if (!_Dic.Keys.Contains(key)) { ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");List<MemberBinding> memberBindingList = new List<MemberBinding>();foreach (var item in typeof(TOut).GetProperties()){PropertyInfo propertyInfo = typeof(TIn).GetProperty(item.Name);if (propertyInfo == null) { continue; }MemberExpression property = Expression.Property(parameterExpression, propertyInfo);memberBindingList.Add(Expression.Bind(item, property));}foreach (var item in typeof(TOut).GetFields()){FieldInfo fieldInfo = typeof(TIn).GetField(item.Name);if (fieldInfo == null) { continue; }MemberExpression property = Expression.Field(parameterExpression, fieldInfo);memberBindingList.Add(Expression.Bind(item, property));}Expression<Func<TIn, TOut>> expression = Expression.Lambda<Func<TIn, TOut>>(Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList), new ParameterExpression[]{parameterExpression});Func<TIn, TOut> func = expression.Compile();_Dic.Add(key,func);}return ((Func < TIn, TOut > )_Dic[key])(tIn);}}static void Main(string[] args){List<ClassLibrary1.PeopleCopy> PeoleCopyList = new List<ClassLibrary1.PeopleCopy>();for (int i = 0; i < 5; i++){ClassLibrary1.People people = new ClassLibrary1.People() { Id = 5+1, Age = 25, Name = "aaa"+i };PeoleCopyList.Add(Class1.ToutGet<ClassLibrary1.People, ClassLibrary1.PeopleCopy>(people));}}

以上這篇C# 表達式目錄樹的應用詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新昌县| 平山县| 庄浪县| 广河县| 泾川县| 晋城| 息烽县| 正镶白旗| 荆州市| 曲松县| 宜城市| 通道| 故城县| 莲花县| 石家庄市| 徐水县| 宝应县| 正镶白旗| 玉树县| 宣汉县| 灵石县| 永定县| 隆回县| 沙河市| 夏河县| 明溪县| 子洲县| 塘沽区| 大宁县| 南投市| 高台县| 常熟市| 敖汉旗| 合作市| 白玉县| 麦盖提县| 横山县| 邢台市| 海伦市| 岳阳市| 聊城市|