Zamial

We have a class that takes another class in the constructor and loops through all the properties on the class passed to it.

Lately it has been decided there are certain cases where different rules should apply.

Currently there is a simple method looping through all properties and performing some work. The same on each. Now that we want a slightly different approach a quick fix is needed for now.

What would be your chosen method for treating each one differently.

I realise we could quickly add a couple of if statements or a case statements to single out the oddities.

How about though and this is in theory.

We create an 'info' attribute that has some entries on it like info (describes the propertr), then some programmatical ones like process (bool value that determines if the property should be processed), then other flags can be added if needed.

The method that loops through properties can now query the attribute values and determine what action to take and then new properties just need to have the attribute added to depict control to the processing class.

What do yu guys think.



Re: Visual C# Language Question re the possible usage of attributes

Gregor Berginc

Zamial,

recently, I was developing a window editor that used PropertyGrid to show properties of selected objects. Attributes were extensively used to control which properties to show and how to treat them. Your task is very similar so I would go for attributes! The only drawback is that reflecting attributes is time consuming if this is frequent operation.

best,





Re: Visual C# Language Question re the possible usage of attributes

Thomas Danecker

Your scenario is a typical usecase of attributes, but as Gregor already stated: Reflection is time consuming.






Re: Visual C# Language Question re the possible usage of attributes

Zamial

Thanks guys nice to hear I wasn't completely barking mad and missing the obvious.