Fill the List of Expressions with the class properties based on list of string

huangapple go评论71阅读模式
英文:

Fill the List of Expressions with the class properties based on list of string

问题

以下是您要翻译的内容:

有一个名为Employee的类,如下所定义:

class Employee
{
	public int Id { get; set; }
	public string Name { get; set; }
	public string Address { get; set; }
}

我需要填写表达式列表,类似于以下方式:

var expressions = new List<Expression<Func<Employee, object>>>()
{
	e => e.Id,
	e => e.Name
}

但这应该基于另一个字符串,即

var fields = new List<string>() { "Id", "Name" };

因此,思路是使用fields字符串列表中存在的字段来填充表达式列表。因此,我们将在Employee类中搜索这些确切的字段名称,并根据此获取属性并填充表达式列表。

英文:

There is a class Employee, defined below:

class Employee
{
	public int Id { get; set; }
	public string Name { get; set; }
	public string Address { get; set; }
}

and I need to fill in the list of expressions, something like below:

var expressions = new List<Expression<Func<Employee, object>>>()
{
	e => e.Id,
	e => e.Name
}

but that should be based on another string which is

var fields = new List<string>() { "Id", "Name" };

So the idea is to fill the expressions list with those fields which are there in the fields string list. Therefore we will search those exact field names in the Employee class and based on that get the property and fill the expressions list.

答案1

得分: 2

弄清楚首先针对一个属性名怎么做。

Expression<Func<Employee, object>> GetExpression(string propertyName) {
    var parameter = Expression.Parameter(typeof(Employee));
    var propertyInfo = typeof(Employee).GetProperty(propertyName);
    if (propertyInfo == null) {
        throw new Exception("没有这个属性!");
    }
    var memberAccess = Expression.Property(parameter, propertyInfo);
    return Expression.Lambda<Func<Employee, object>>(
        propertyInfo.PropertyType.IsValueType ?
            Expression.Convert(memberAccess, typeof(object)) :
            memberAccess, 
        parameter);
}

请注意,如果属性的类型是值类型,你需要一个 Expression.Convert。这是为了将值装箱为 object

然后,你可以简单地选择名称列表:

List<Expression<Func<Employee, object>>> GetExpressions(List<string> propertyNames) =>
    propertyNames.Select(n => GetExpression(n)).ToList();
英文:

Figure out how to do this for one property name first.

Expression<Func<Employee, object>> GetExpression(string propertyName) {
    var parameter = Expression.Parameter(typeof(Employee));
    var propertyInfo = typeof(Employee).GetProperty(propertyName);
    if (propertyInfo == null) {
        throw new Exception("No such property!");
    }
    var memberAccess = Expression.Property(parameter, propertyInfo);
    return Expression.Lambda<Func<Employee, object>>(
        propertyInfo.PropertyType.IsValueType ?
            Expression.Convert(memberAccess, typeof(object)) :
            memberAccess, 
        parameter);
}

Note that you will need an Expression.Convert if the type of the property is a value type. This is to box the value into an object.

Then you can just Select the list of names:

List<Expression<Func<Employee, object>>> GetExpressions(List<string> propertyNames) =>
    propertyNames.Select(n => GetExpression(n)).ToList();

huangapple
  • 本文由 发表于 2023年5月18日 13:05:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定