怎么安装ExpressionTreeVisualizer for Visual Studio 2019
1、下载 ExpressionTreeVisualizer https://github.com/zspitz/ExpressionTreeVisualizer/releases , 解压后把相应dll文件拷贝相应的目录
2、拷贝到以下路径之中的任何一个。
-
sualStudioInstallPath
\Common7\Packages\Debugger\Visualizers(这个是路径模板格式) 例如:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Packages\Debugger\Visualizers
-
My Documents\
VisualStudioVersion\Visualizers
(这个是路径模板格式)
-
3、拷贝到以下路径之中的任何一个。
-
VisualStudioInstallPath
\Common7\Packages\Debugger\Visualizers\
Framework例如:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Packages\Debugger\Visualizers\Framework
-
-
My Documents\
VisualStudioVersion\Visualizers\
Framework
where Framework is either:
net2.0
for debuggees running the.NET Framework
runtimenetstandard2.0
for debuggees using a runtime that supportsnetstandard 2.0
(.NET Framework v4.6.1+
or.NET Core 2.0+
).netcoreapp
for debuggees running the.NET Core
runtime. (supports.NET Core 2.0+
)(我是用这个框架我就拷贝这个框架下面)
测试案例:
大幅度反对法
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; namespace TestVisualizer { class Program { static void Main(string[] args) { List<PeopleCopy> peoleCopyList = new List<PeopleCopy>(); for (int i = 0; i < 5; i++) { People people = new People() { Id = 5 + 1, Age = 25, Name = "aaa" + i }; peoleCopyList.Add(ExpressionTree.TransExp<People, PeopleCopy>(people)); } foreach (var item in peoleCopyList) { Console.WriteLine(item); } Console.Read(); } } 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 override string ToString() { return "Age=" + Age + ";Name=" + Name + ";Id=" + Id; } } public class ExpressionTree { private static Dictionary<string, object> _Dic = new Dictionary<string, object>(); public 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); } } }
编程是个人爱好