获取具有反射的属性

时间:2014-11-26 10:03:54

标签: c# reflection

这是我的Xaml.cs中的一个方法:

void itemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.UsingLogicalPageNavigation())
            {
                this.navigationHelper.GoBackCommand.RaiseCanExecuteChanged();
            }

            // Here is the object which properties i am trying to get:
            var mySelectedItem = e.AddedItems[0];

            //Here I would like to acess the properties

        }

我试图用反射从mySelected Item中获取属性。 一直在尝试:

PropertyInfo property = GetType().GetProperty(propertyName) <--Cant resolve GetProperty()

object value = propertyInfo.GetValue(mySelectedItem);<--Cant resolve PropertyInfo

在这种情况下,是否不可能使用refelction?

1 个答案:

答案 0 :(得分:1)

你不需要反思你想要的东西。

将对象投射到正确的对象时,可以访问对象的属性。

void itemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (this.UsingLogicalPageNavigation())
        {
            this.navigationHelper.GoBackCommand.RaiseCanExecuteChanged();
        }
        var mySelectedItem = e.AddedItems[0];

        MyObject myObject = (MyObject)mySelectedItem;

        // Now you can access your property as follows: myObject.MyProperty;

    }