Home All Groups Group Topic Archive Search About

Property reflection

Author
4 Nov 2006 11:25 AM
Saso
Hello!

I was wondering if there is a some way to mark a class property,
without modifiers use. By using reflection, I know how to get all properties
that returns int, but how to get only specific int properties?

See example below:

public class Test
{
    private int _A; 
    private int _B;

    public int A
    {
        get { return (_A); }
        set { _A = value; }
    }

    (for example, I want to mark this property)
    public int B
    {
        get { return (_B); }
        set { _B = value; }
    }   
}

Object o = new Test();
PropertInfo[] properties = o.GetType().GetProperties();

I want to show only marked int properties? All these properties must be
accessible, so the way with modifiers public/protected is not useful.
I've done this with string array, so I have a condition inside loop that
checks if array contains a property. But I don't like doing it this way,
because I have a lot of classes and I think this is not the clearest way.

I will appreciate any suggestion.

Regards
Saso

Author
4 Nov 2006 1:40 PM
mccoyn
It sounds like you need attributes.  This link describes them.
http://msdn2.microsoft.com/en-us/library/z0w1kczw(VS.80).aspx

Another possibility is to write a function in each class that returns an
array PropertyInfo.  This will allow each class to do the filtering.  I think
the attributes thing will be cleaner though.

Show quote
"Saso" wrote:

> Hello!
>
> I was wondering if there is a some way to mark a class property,
> without modifiers use. By using reflection, I know how to get all properties
> that returns int, but how to get only specific int properties?
>
> See example below:
>
> public class Test
> {
>     private int _A; 
>     private int _B;
>    
>     public int A
>     {
>         get { return (_A); }
>         set { _A = value; }
>     }
>    
>     (for example, I want to mark this property)
>     public int B
>     {
>         get { return (_B); }
>         set { _B = value; }
>     }   
> }
>
> Object o = new Test();
> PropertInfo[] properties = o.GetType().GetProperties();
>
> I want to show only marked int properties? All these properties must be
> accessible, so the way with modifiers public/protected is not useful.
> I've done this with string array, so I have a condition inside loop that
> checks if array contains a property. But I don't like doing it this way,
> because I have a lot of classes and I think this is not the clearest way.
>
> I will appreciate any suggestion.
>
> Regards
> Saso
Author
4 Nov 2006 5:18 PM
mccoyn
Some sample code that creates a checkbox for each property and checks the
ones we've flaged using a custom attribute.:

    // AutomatedPropety attribute will identify the
    // properties we want to look for.
    [AttributeUsage(AttributeTargets.Property)]
    class AutomatedPropertyAttribute
        : Attribute
    {    }

        // Create a checkbox for each property and check the ones that
        // have AutomatedPropertyAttribute.
        private void Form1_Load(object sender, EventArgs e)
        {
            int top = 0;
            foreach (PropertyInfo property in typeof(ClassA).GetProperties())
            {
                CheckBox checkbox = new CheckBox();
                checkbox.Top = top;
                top = checkbox.Bottom;
                checkbox.Text = property.Name;
                checkbox.Checked = (property.GetCustomAttributes(
                    typeof(AutomatedPropertyAttribute), true).Length > 0);
                checkbox.Parent = this;
            }
        }

    // This class has some properties that have been flagged
    // with AtutomatedPropertyAttribute.
    class ClassA
    {
        [AutomatedProperty]
        public int PropertyA
        {
            get{return mPropertyA;}
            set{mPropertyA = value;}
        }

        public int PropertyB
        {
            get { return mPropertyA; }
            set { mPropertyA = value; }
        }

        public int PropertyC
        {
            get { return mPropertyA; }
            set { mPropertyA = value; }
        }

        [AutomatedProperty]
        public int PropertyD
        {
            get { return mPropertyA; }
            set { mPropertyA = value; }
        }

        private int mPropertyA;
    }

Show quote
"mccoyn" wrote:

> It sounds like you need attributes.  This link describes them.
> http://msdn2.microsoft.com/en-us/library/z0w1kczw(VS.80).aspx
>
> Another possibility is to write a function in each class that returns an
> array PropertyInfo.  This will allow each class to do the filtering.  I think
> the attributes thing will be cleaner though.
>
> "Saso" wrote:
>
> > Hello!
> >
> > I was wondering if there is a some way to mark a class property,
> > without modifiers use. By using reflection, I know how to get all properties
> > that returns int, but how to get only specific int properties?
> >
> > See example below:
> >
> > public class Test
> > {
> >     private int _A; 
> >     private int _B;
> >    
> >     public int A
> >     {
> >         get { return (_A); }
> >         set { _A = value; }
> >     }
> >    
> >     (for example, I want to mark this property)
> >     public int B
> >     {
> >         get { return (_B); }
> >         set { _B = value; }
> >     }   
> > }
> >
> > Object o = new Test();
> > PropertInfo[] properties = o.GetType().GetProperties();
> >
> > I want to show only marked int properties? All these properties must be
> > accessible, so the way with modifiers public/protected is not useful.
> > I've done this with string array, so I have a condition inside loop that
> > checks if array contains a property. But I don't like doing it this way,
> > because I have a lot of classes and I think this is not the clearest way.
> >
> > I will appreciate any suggestion.
> >
> > Regards
> > Saso

AddThis Social Bookmark Button