SoFunction
Updated on 2025-03-07

Detailed explanation of the application of C# expression directory tree

Use the expression directory tree to implement two different types of attribute assignments:

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.(key)) { 
ParameterExpression parameterExpression = (typeof(TIn), "p");
List<MemberBinding> memberBindingList = new List<MemberBinding>();
foreach (var item in typeof(TOut).GetProperties())
{
PropertyInfo propertyInfo = typeof(TIn).GetProperty();
if (propertyInfo == null) { continue; }
MemberExpression property = (parameterExpression, propertyInfo);
((item, property));
}
foreach (var item in typeof(TOut).GetFields())
{
FieldInfo fieldInfo = typeof(TIn).GetField();
if (fieldInfo == null) { continue; }
MemberExpression property = (parameterExpression, fieldInfo);
((item, property));
}
Expression<Func<TIn, TOut>> expression = <Func<TIn, TOut>>(((typeof(TOut)), memberBindingList), new ParameterExpression[]
{
parameterExpression
});
Func<TIn, TOut> func = ();
_Dic.Add(key,func);
}
return ((Func < TIn, TOut > )_Dic[key])(tIn);
}
}
static void Main(string[] args)
{
List<> PeoleCopyList = new List<>();
for (int i = 0; i < 5; i++)
{
 people = new () { Id = 5+1, Age = 25, Name = "aaa"+i };
(<, >(people));
}
}

The above detailed explanation of the application of the C# expression directory tree is all the content I share with you. I hope you can give you a reference and I hope you can support me more.