强类型属性声明 - 这段代码安全吗?

发布于 2024-10-04 03:42:55 字数 1069 浏览 3 评论 0原文

我想知道以下代码是否“安全”。我所说的“安全”是指我不依赖于某些特定的编译器版本或未记录的功能。 我想获取带有属性/字段名称的字符串,但我想使用强类型来声明它(我希望编译器检查特定字段/属性是否存在)。 我的方法如下所示:

string GetPropertyName<T>(Expression<Func<T, object>> expression)
{
    if (expression.Body is UnaryExpression)
    {
        var operand = ((UnaryExpression)expression.Body).Operand.ToString();
        return operand.Substring(operand.IndexOf(".") + 1);
    }
    else if (expression.Body is MemberExpression)
    {
        return ((MemberExpression)expression.Body).Member.Name;
    }
    else
    {
        throw new NotImplementedException();
    }            
}

这就是我想要使用它的方式:

class Foo
{
    public string A { get; set; }
    public Bar B { get; set; }
}

class Bar
{
    public int C { get; set; }
    public Baz D { get; set; }
}

class Baz
{
    public int E { get; set; }
}


GetPropertyName<Foo>(x => x.A)
GetPropertyName<Foo>(x => x.B)
GetPropertyName<Foo>(x => x.B.C)
GetPropertyName<Foo>(foo => foo.B.D.E)

提前感谢您的帮助。

I'm wondering if following code is "safe". By "safe" I mean that I don't depend on some specific compiler version or undocumented feature.
I want to get string with property / field name, but I want to declare it using strong typing (I want the compiler to check if specific field / property exists).
My method looks like this:

string GetPropertyName<T>(Expression<Func<T, object>> expression)
{
    if (expression.Body is UnaryExpression)
    {
        var operand = ((UnaryExpression)expression.Body).Operand.ToString();
        return operand.Substring(operand.IndexOf(".") + 1);
    }
    else if (expression.Body is MemberExpression)
    {
        return ((MemberExpression)expression.Body).Member.Name;
    }
    else
    {
        throw new NotImplementedException();
    }            
}

And here is how I want to use it:

class Foo
{
    public string A { get; set; }
    public Bar B { get; set; }
}

class Bar
{
    public int C { get; set; }
    public Baz D { get; set; }
}

class Baz
{
    public int E { get; set; }
}


GetPropertyName<Foo>(x => x.A)
GetPropertyName<Foo>(x => x.B)
GetPropertyName<Foo>(x => x.B.C)
GetPropertyName<Foo>(foo => foo.B.D.E)

Thanks in advance for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

壹場煙雨 2024-10-11 03:42:55

我不确定 < 的输出code>ToString 方法以任何方式得到保证。文档只是说它“返回表达式的文本表示”。

(我怀疑输出不太可能在不同的平台/版本之间改变,但是当你的目标是使用强类型、编译时检查等时,我有点不愿意依赖它。)

这是我的方法,它做了一些事情不使用 ToString 类似:

public static string GetPropertyName<T>(Expression<Func<T, object>> e)
{
    MemberExpression me;
    switch (e.Body.NodeType)
    {
        case ExpressionType.Convert:
        case ExpressionType.ConvertChecked:
            var ue = e.Body as UnaryExpression;
            me = ((ue != null) ? ue.Operand : null) as MemberExpression;
            break;
        default:
            me = e.Body as MemberExpression;
            break;
    }

    if (me == null)
        throw new ArgumentException("Expression must represent field or property access.", "e");

    var stack = new Stack<string>();

    do
    {
        stack.Push(me.Member.Name);
        me = me.Expression as MemberExpression;
    } while (me != null);

    return string.Join(".", stack);    // use "stack.ToArray()" on .NET 3.5
}

I'm not sure that the output of the ToString method is guaranteed in any way. The documentation just says that it "returns a textual representation of the Expression".

(I suspect that the output is unlikely to change across different platforms/versions, but I'd be a bit reluctant to rely on it when your aim is to use strong typing, compile-time checks etc.)

Here's my method that does something similar without using ToString:

public static string GetPropertyName<T>(Expression<Func<T, object>> e)
{
    MemberExpression me;
    switch (e.Body.NodeType)
    {
        case ExpressionType.Convert:
        case ExpressionType.ConvertChecked:
            var ue = e.Body as UnaryExpression;
            me = ((ue != null) ? ue.Operand : null) as MemberExpression;
            break;
        default:
            me = e.Body as MemberExpression;
            break;
    }

    if (me == null)
        throw new ArgumentException("Expression must represent field or property access.", "e");

    var stack = new Stack<string>();

    do
    {
        stack.Push(me.Member.Name);
        me = me.Expression as MemberExpression;
    } while (me != null);

    return string.Join(".", stack);    // use "stack.ToArray()" on .NET 3.5
}
山人契 2024-10-11 03:42:55

我认为你的代码没问题。我没有看到任何问题。要深入了解这一点,我建议您阅读 本文这个,也是。

I think you code is okay. I don't see any problems. To get a little deep into this, I recommend you read this article and this one, too.

ζ澈沫 2024-10-11 03:42:55
    public static string GetPropertyName<T>(Expression<Func<T, object>> e)
    {
        if (e.Body is MemberExpression)
            return ((MemberExpression)e.Body).Member.Name;
        else if (e.Body is UnaryExpression)
            return ((MemberExpression)((UnaryExpression)e.Body).Operand).Member.Name;

        throw new ArgumentException("Expression must represent field or property access.", "e");
    }
    public static string GetPropertyName<T>(Expression<Func<T, object>> e)
    {
        if (e.Body is MemberExpression)
            return ((MemberExpression)e.Body).Member.Name;
        else if (e.Body is UnaryExpression)
            return ((MemberExpression)((UnaryExpression)e.Body).Operand).Member.Name;

        throw new ArgumentException("Expression must represent field or property access.", "e");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文