当前位置:编程学习 > C#/ASP.NET >>

PropertyInfo.SetValue类型问题


public class Test
{
    private object _A;
    public string A
    {
        get
        {
            if (_A is Binding)
                return (string)((Binding)_A).GetValue();
            return (string)_A;
        }
        set
        {
            _A = value;
        }
    }
}

public class Binding
{
    public object GetValue() { return null; }
}


static void Main(string[] args)
{
    Test test = new Test();
    typeof(Test).GetProperty("A").SetValue(test, new Binding());
    //报错
}

以上反射会先检查类型,直接报错。如何解决? --------------------编程问答-------------------- test, new Binding(),test和Binding啥关系 --------------------编程问答--------------------
引用 1 楼 bdmh 的回复:
test, new Binding(),test和Binding啥关系

Binding只是作为一个提供值的基类。
Test里的属性先要判断是不是Binding,是Binding就返回Binding的GetValue的值,不是就直接返回值 --------------------编程问答-------------------- public class Test
{
    private object _A;
    public object A
    {
        get
        {
            ... --------------------编程问答--------------------
引用 3 楼 gomoku 的回复:
public class Test
{
    private object _A;
    public object A
    {
        get
        {
            ...

但是属性类型不可能只用object啊……

谁知道WPF的依赖属性在XAML上是怎么实现的么,WPF的一些绑定会生成BindingExpressionBase,和CLR属性类型不一样,但是载入不会出类型不符的异常…… --------------------编程问答-------------------- test.A = new Binding();  //这能行吗 --------------------编程问答-------------------- typeof(Test).GetField("_A").SetValue(test, new Binding());
WPF绑定也是对内部的依赖项属性进行设置,而依赖项属性都是用object类型存储的,自然不会出现异常。 --------------------编程问答--------------------
引用 5 楼 xiaozhi_5638 的回复:
test.A = new Binding();  //这能行吗

一定要用反射,用的是System.Windows.Markup.XamlReader.Load()。这不是我能控制的…… --------------------编程问答--------------------
引用 6 楼 qldsrx 的回复:
typeof(Test).GetField("_A").SetValue(test, new Binding());
WPF绑定也是对内部的依赖项属性进行设置,而依赖项属性都是用object类型存储的,自然不会出现异常。

WPF是如何使得XamlReader.Load方法不使用反射去设置属性?
比如这样的数据。

<Test A="{Binding XXX}" />

用XamlReader.Load时,它是直接使用反射去SetValue的,会直接报错。 --------------------编程问答-------------------- 你会不会提问啊,你有说是WPF里面使用吗?WPF里面的属性是依赖项属性,你先学学什么叫依赖项属性,根本不是你那样定义的。依赖项属性的赋值和获取根本不使用反射,那样效率太低了。 --------------------编程问答--------------------
引用 9 楼 qldsrx 的回复:
你会不会提问啊,你有说是WPF里面使用吗?WPF里面的属性是依赖项属性,你先学学什么叫依赖项属性,根本不是你那样定义的。依赖项属性的赋值和获取根本不使用反射,那样效率太低了。

我的意思是,我的类里面定义的属性,XAML是直接通过CLR属性设置的,然后出错。出现的错误和PropertyInfo.SetValue一样。


    [ContentProperty("ItemsSource")]
    public class ItemsControl : Control
    {
        public ItemsControl()
        {
            _Items = new ItemCollection(this);
        }

        public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(ItemsControl));
        public DataTemplate ItemTemplate { get { return (DataTemplate)GetValue(ItemTemplateProperty); } set { SetValue(ItemTemplateProperty, value); } }
        
        public static readonly DependencyProperty ItemTemplateSelectorProperty = DependencyProperty.Register("ItemTemplateSelector", typeof(DataTemplateSelector), typeof(ItemsControl));
        public DataTemplateSelector ItemTemplateSelector { get { return (DataTemplateSelector)GetValue(ItemTemplateSelectorProperty); } set { SetValue(ItemTemplateSelectorProperty, value); } }

        private ItemCollection _Items;
        [Category("Common Properties")]
        public ItemCollection Items { get { return _Items; } }

        internal Panel ItemsPanelElement;
        public static readonly DependencyProperty ItemsPanelProperty = DependencyProperty.Register("ItemsPanel", typeof(ItemsPanelTemplate), typeof(ItemsControl));
        public ItemsPanelTemplate ItemsPanel { get { return (ItemsPanelTemplate)GetValue(ItemsPanelProperty); } set { SetValue(ItemsPanelProperty, value); } }

        public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ItemsControl));
        [Category("Common Properties")]
        public IEnumerable ItemsSource { get { return (IEnumerable)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } }


    }


--------------------编程问答--------------------
引用 9 楼 qldsrx 的回复:
你会不会提问啊,你有说是WPF里面使用吗?WPF里面的属性是依赖项属性,你先学学什么叫依赖项属性,根本不是你那样定义的。依赖项属性的赋值和获取根本不使用反射,那样效率太低了。

看来还是得要写一个TypeDescriptionProvider和ICustomTypeDescriptor是吧……
--------------------编程问答-------------------- 你给的几张图,真的没看出有什么必然的联系。
WPF中的Binding是通过SetBinding与依赖项属性关联的,而不是作为值赋值给某个属性,你却将自己定义的一个名为Binding的类,作为值赋值给某个属性,自然要出错,运行原理是不同的。 --------------------编程问答-------------------- 问题错在,Test.A属性为STRING,而typeof(Test).GetProperty("A").SetValue(test, new Binding());是将对象(new Binding())赋给STRING,肯定报错的!不支持隐式转换,需将Test.A属性改为object即可 --------------------编程问答--------------------
引用 12 楼 qldsrx 的回复:
你给的几张图,真的没看出有什么必然的联系。
WPF中的Binding是通过SetBinding与依赖项属性关联的,而不是作为值赋值给某个属性,你却将自己定义的一个名为Binding的类,作为值赋值给某个属性,自然要出错,运行原理是不同的。

同样是XamlReader.Load,WPF是怎么把XAML里的绑定属性值绑定到Object的?
我现在写了个TypeDescriptionProvider,但是没有被调用…… --------------------编程问答-------------------- PropertyInfo.PropertyType.Name你是不是要这个属性的声明类型名称哇?
取到类型名称后,就可以判断转换你想要的动作了?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,