如何使用反射来获取属性 [英] How can I use reflection to get properties

查看:56
本文介绍了如何使用反射来获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



这个问题看似简单,但我发现在获取高级层次中定义的所有属性方面存在一些小问题(这意味着我不希望通过这种方法获得继承的属性。)



这是类:

Hi everybody,

The question may seem easy, but I found some little problems in fetching all and just properties defined in the high-level hierarcy (it means I do not want inherited properties to be get by this method).

This is the class:

public partial class GUIButton : CloneableGUIElement
    {
        private Rect _position;
        /// <summary>
        /// Position and size of the GUIButton
        /// </summary>
        public Rect Position
        {
            get { return _position; }
            set
            {
                Canvas.SetLeft(this, value.X);
                Canvas.SetTop(this, value.Y);
                this.Height = value.Height;
                this.Width = value.Width;
                _position = value;
            }
        }

        private string _text;
        /// <summary>
        /// Text to be included inside the GUIButton
        /// </summary>
        public string Text
        {
            get { return _text; }
            set
            {
                this.Button.Content = value;
                _text = value;
            }
        }

        private string _image;
        /// <summary>
        /// Texture to include inside the GUIButton as a background
        /// </summary>
        public string Image
        {
            get { return _image; }
            set
            {
                VisualBrush b = new VisualBrush(new Image() { Source = new BitmapImage(new Uri(value)), Stretch = Stretch.Fill });
                this.Button.Background = b;
                _image = value;
            }
        }

        //TODO: Content & Style

        public override object Clone()
        {
            return new GUIButton(this.Position, this.Text, this.Image);
        }
        public GUIButton()
        {
            InitializeComponent();
        }
        public GUIButton(Rect position, string text, string image)
        {
            InitializeComponent();
            Position = position;
            Text = text;
            Image = image;
        }
    }





其中 CloneableGUIElement 派生自UserControl。



我试过这个





Where CloneableGUIElement derives from UserControl.

I tried this

PropertyInfo[] PIs = typeof(GUIButton).GetProperties(BindingFlags.DeclaredOnly);





但是数组是空的。即使我把BindingFlag作为参数(我已经检查过),它仍然是空的,但如果我不使用任何BindingFlag条件,它会返回106个属性(继承而不是继承的属性)...



如何从GUIButton获得位置,文本和图像?



非常感谢!!



LG



but the array is empty. It remains empty even if I put whatever BindingFlag as argument (I've checked), but it returns 106 properties (inherited and not inherited ones) if I do not use any BindingFlag conditions...

How can I get just Position, Text and Image from GUIButton?

Thanks a lot!!

LG

推荐答案

我认为你应该简单地优化搜索。使用 BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance 应该这样做。
I think you should simply refine the search. Using BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance should do it.


这篇关于如何使用反射来获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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