Click here to Skip to main content
15,881,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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;
        }
    }


Where CloneableGUIElement derives from UserControl.

I tried this

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


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
Posted
Comments
PIEBALDconsult 21-Sep-14 14:50pm    
I don't see why you would want a class to use Reflection to get its own members.
Have you looked into writing a copy constructor?
LLLLGGGG 21-Sep-14 14:55pm    
I do not want to get properties from the class GUIButton, but from antoher one... and as if I have to write a lot of different classes (such as GUISlider, which does not have the text property), I would like to have just one method to get properties of the class as I will use polymorphism to assign a GUI*** (where *** stands for some element) to a CloneableGUIElement and as the original type is not modified by this assignment, I will use the getType method to get the properties. I need it to make a properties window such as the one included in Visual Studio.

1 solution

I think you should simply refine the search. Using BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance should do it.
 
Share this answer
 
Comments
LLLLGGGG 21-Sep-14 16:20pm    
Quick, simple answer! Thanks a lot! It works for me! So I have to use multiple conditions unless it won't work... Another codesnippet to put in my Favourites.cs file...

Thanks for your help!

LG
Zoltán Zörgő 21-Sep-14 16:37pm    
You are welcome.
Sergey Alexandrovich Kryukov 21-Sep-14 19:49pm    
Sure, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900