将属性列表作为强类型参数传递

时间:2015-11-10 13:18:23

标签: c#

我有这个方法从表达式中提取属性名称:

private static string GetPropertyName<TObj, TProp>(Expression<Func<TObj, TProp>> prop)
{
    var expression = prop.Body as MemberExpression;

    if (expression != null)
    {
        var property = expression.Member as PropertyInfo;

        if (property != null)
        {
            return property.Name;
        }
    }

    return string.Empty;
}

所以稍后我可以像这样使用它:

GetPropertyName((User u) => u.Surname); //Returns "Surname"

我希望能够逐个传递一组属性。需要明确的是,这些属性可能有不同的类型。

3 个答案:

答案 0 :(得分:2)

我完全赞同@Patrick及其首选方式。

但如果您说您没有使用C#6.0,那么您可以使用您编写的代码。我只使用CREATE TABLE Tasks(SortOrder VARCHAR(100)); IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE [TABLE_NAME] = 'Tasks' AND [COLUMN_NAME] = 'SortOrder' AND IS_NULLABLE = 'NO' AND DATA_TYPE = 'INT') BEGIN ALTER TABLE [Tasks] ALTER COLUMN [SortOrder] INT NOT NULL END param和一个yield return循环

foreach

<强>更新

First One要求您再次具体指定对象的类型,这意味着您必须再次提供全长表达式。

尝试下面的内容,它会要求您在一个表达式中尽可能多地指定属性。

    private static IEnumerable<string> GetPropertyName<TObj, TProp>(params Expression<Func<TObj, TProp>>[] propCollection)
    {

        foreach (var prop in propCollection)
        {

            var expression = prop.Body as MemberExpression;

            if (expression != null)
            {
                var property = expression.Member as PropertyInfo;

                if (property != null)
                {
                    yield return property.Name;
                }
            }
            yield return string.Empty;
        }

    }

您可以将其称为 -

public static IEnumerable<string> GetPropertiesName<TObj, TProp>(Expression<Func<TObj, TProp[]>> prop)
{
    var array = (prop.Body as NewArrayExpression);
    var exp = array == null ? null : array.Expressions;

    if (exp != null)
    {
        //var expArr = (prop.Body as NewArrayExpression).Expressions;

        foreach (var oneProp in exp)
        {
            Expression onePropExp;
            if (oneProp.GetType() == typeof (UnaryExpression))
            {
                onePropExp = (oneProp as UnaryExpression).Operand;
            }
            else
            {
                onePropExp = oneProp;
            }
            var property = (onePropExp as MemberExpression).Member as PropertyInfo;

            if (property != null)
            {
                yield return property.Name;
            }
            yield return string.Empty;
        }

    }
    yield return string.Empty;
}

答案 1 :(得分:1)

可能是我,但我认为你不需要这么做。您只需使用C#6 nameof关键字即可。这假设您当然可以使用C#6。

string name = nameof(u.Surname);

答案 2 :(得分:0)

试试这个:

用法:string[] props = GetPropertiesName((MainWindow m) => m.Lalala, (MainWindow m) => m.Lalala);

private static string[] GetPropertiesName<TObj, TProp>(params Expression<Func<TObj, TProp>>[] prop)
{
    List<string> ret = new List<string>();
    foreach (var item in prop)
        ret.Add(GetPropertyName(item));
    return ret.ToArray();
}
private static string GetPropertyName<TObj, TProp>(Expression<Func<TObj, TProp>> prop)
{
    var expression = prop.Body as MemberExpression;

    if (expression != null)
    {
        var property = expression.Member as PropertyInfo;

        if (property != null)
        {
            return property.Name;
        }
    }

    return string.Empty;
}