什么是物业信息的优势在哪里? [英] what's the advantage of Property Info?

查看:104
本文介绍了什么是物业信息的优势在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET Framework 3.5的,我们可以得到的属性信息使用下述代码

In .net FrameWork 3.5 we can get the Property Information using below mentioned code.

using System;
using System.Linq.Expressions;
using System.Reflection;

class Foo
{
    public string Bar { get; set; }
}
static class Program
{
    static void Main()
    {
        PropertyInfo prop = PropertyHelper<Foo>.GetProperty(x => x.Bar);
    }
}
public static class PropertyHelper<T>
{
    public static PropertyInfo GetProperty<TValue>(
        Expression<Func<T, TValue>> selector)
    {
        Expression body = selector;
        if (body is LambdaExpression)
        {
            body = ((LambdaExpression)body).Body;
        }
        switch (body.NodeType)
        {
            case ExpressionType.MemberAccess:
                return (PropertyInfo)((MemberExpression)body).Member;
            default:
                throw new InvalidOperationException();
        }
    }
}

这也可以通过进行创建一个类的实例和访问属性成员。那么什么是物业信息的优势在哪里?

This can also be done by creating the instance of a class and access the Property Member. So what's the advantage of Property Info?

推荐答案

的PropertyInfo 用于获取类的属性信息。创建实例是没有必要的。优点是,它消除打字错误的可能性。

PropertyInfo is used to get information of properties of class. Creating an instance is not needed. Advantage is that it removes possibility of typing error.

表达式是完全不同的概念(即使用反射内部)。表达式用于表示方法体作为树结构。这允许创建/在运行时调整方法定义的灵活性。

Expressions are entirely different concept (that uses Reflection internally). Expressions are used to represent method body as tree structure. This allows flexibility for creating/tweaking method definition at runtime.

表达式的这种能力是通过的可查询类构建/远程源执行动态查询。

This capability of Expressions is harnessed by Queryable class to build/execute dynamic queries at remote source.

为例,
考虑 INotifyPropertyChanged的接口。它用于属性更改通知。

Example, Consider INotifyPropertyChanged interface. It is used for property change notification.

通常的实现需要的属性名称作为字符串参数。因此,在运行时检测到输入错误。也重构可以打破代码(虽然智能重构工具需要照顾这一点)。

Usual implementation takes property name as string parameter. Thus typing error are detected at runtime. Also Refactoring can break the code (Though Smart refactor tool takes care of this).

    void RaisePropertyChanged(PropertyChangedEventArgs args)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

    public string Name
    {
        set
        {
            _name = value;
            RaisePropertyChanged("Name"); // Property name is specified as string
        }
    }

一个更好的实施(不是性能高效虽然)接受属性名作为表达式

A better implementation (not the performance efficient though) takes property name as Expression.

    void RaisePropertyChanged<T>(Expression<Func<T>> selectorExpression)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            MemberExpression body = selectorExpression.Body as MemberExpression;

            handler(this, new PropertyChangedEventArgs(body.Member.Name));
        }
    }

    public string Name
    {
        set
        {
            _name = value;
            RaisePropertyChanged( () => this.Name); // Property is specified instead of name that removes typing error
        }
    }

这篇关于什么是物业信息的优势在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆