Using Attributes
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
If you are doing anything with Custom Controls, for use by others, it is pretty difficult to get by without using Attributes. For example the
DesignerAttribute
.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? - Because 31 Oct = 25 Dec. Business Myths of the Geek #4 'What you think matters.'
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
I have a WPF/C# app which is a main window and loads in other "apps" as requested, each "app" is essentially a WPF page the code behind class for each is marked with an attribute which details the name of the app, a brief description, the location of the xaml, if the app can have multiple occurences etc. The info is only ever retrieved once, when the main app is loaded, so performance is not an issue.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
I have an application whose core functionality is built and distributed to all sites. However, the reports are customized for each site and provided in a separate dll. The application menus are created by dynamically loading the referenced dll's and polling some defined interfaces for the menus. Although, I could have used a non-attribute based solution for the piece that dynamically creates the menus it made it easier for other programmers on the time to make small changes without having to understand the entire system. Plus, it had the advantage of already dynamically creating the types for the reports so they could be loaded on request which already required reflection. The other choice was to have 5 files that each had to be manually modified every time a new report was created. In retrospect, since the reflection is only done on application load, the performance hit from using reflection didn't matter and the maintenance ended up being easier. I think the lesson learned is attributes and reflection are best used in projects where the maintenance will be performed by external parties or by less experienced developers or in an organization with poor development documentation. They are a hack when good design is hindered by other, usually, external factors.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
Marshal(l)ing?
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
Richard Blythe wrote:
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection.
The performance hit isn't as bad as it used to be (post .net 3.0). A lot of the new .net 4.0 stuff is reflected (and some of the 3.5), so they sorted the performance out before doing this. IMO code clarity wins out over performance, unless there is a specific need for for the performance to be good (e.g. bulk processing, real-time processing) etc. The other posters have given good reasons for attributes, WCF wouldn't work and it is good for the entity framework too.
ragnaroknrol The Internet is For Porn[^]
Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners. -
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
Sure, the performance isn't great, but proper use of attributes can make for some VERY clean and maintainable code. Improper use, well... There are plenty of ways to screw up a design, and you really don't need attributes if you want to make spaghetti code :) I minimize the performance hit by just reading once and caching... The reflection isn't a big deal unless you're doing it repeatedly. Want an example? Well, I have a set of data objects that act as a sort of bridge between multiple external data sources... Each one has different naming conventions, and there's a good deal of overlap... So instead of writing detailed converters for each source, I have stuff like this:
[Source(Sources.Database, "TABLE1.SHORT_NAME", NullIfEmpty=true)]
[Source(Sources.OtherThing, "ItemName")]
public string Name { get; internal set; }(The names have been changed to protect the innocent) I have a "SourcePathMap" class, which, given a "Sources" value, goes through all classes in a certain namespace that are a subset of "Record", and builds several hashes that translate field names to property setters. Then each source has a class or classes to use those maps to translate to and from the external API. So basically, instead of having separate files to translate each of these, I sacrifice a little performance to be able to look at a data object and say "Well, this field comes from X in source Y, or from A in source B" and so on. Easy to read, easy to maintain.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
I assume you mean custom attributes. As with my enum utilities[^], I use them often, but I try to read them once and cache them.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
I prefer using C++. C++ doesn't have attributes or reflection. Problem solved. (For my C# code, I do use attributes and reflection very heavily in a test program, but rarely elsewhere. Due to nature of what I do, I rarely need to serialize anything so I don't need attributes for that. I endorse your approach. Even if reflection is faster, actually designing your code with performance in mind instead of using every damn feature of C# in a knee jerk fashion is a Very Good Thing and way to uncommon in my experience.)
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
For web services or Win32 API calls from C# environment. In general the attributes confuses me.
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
I've only used attributes for three things: 1. To tell the .NET serializer what to do 2. To tell the .NET Form Designer what do to 3. To tell my Interacx application, at the server, what plugin methods are associated with what views and CRUD operations, which is all figured out at startup. [soapbox] I think attributes are pretty much evil. I've seen them used to couple class names and properties with database tables and fields, which in my opinion is mixing declarative information, better represented externally, in the imperative language code base. Yuck. So when using attributes, I always ask myself, am I applying an attribute that is actually completely decoupled from the imperative code and whose values may vary independently of the code, or vice versa? If so, then attributes are an incorrect pattern to apply. Yes, it may be simpler, but I prefer to do the extra work up front rather than be royally screwed later. And it really is amazing how a little defensive design up front can create very robust applications. [/soapbox] Marc
-
I've only used attributes for three things: 1. To tell the .NET serializer what to do 2. To tell the .NET Form Designer what do to 3. To tell my Interacx application, at the server, what plugin methods are associated with what views and CRUD operations, which is all figured out at startup. [soapbox] I think attributes are pretty much evil. I've seen them used to couple class names and properties with database tables and fields, which in my opinion is mixing declarative information, better represented externally, in the imperative language code base. Yuck. So when using attributes, I always ask myself, am I applying an attribute that is actually completely decoupled from the imperative code and whose values may vary independently of the code, or vice versa? If so, then attributes are an incorrect pattern to apply. Yes, it may be simpler, but I prefer to do the extra work up front rather than be royally screwed later. And it really is amazing how a little defensive design up front can create very robust applications. [/soapbox] Marc
Applause... Great speech Marc. :-D All joking aside, I completely agree with your assesment of attributes. I don't think my lack of attributes comes from a lack of knowlege. I just haven't had a lot of projects that required them.
The mind is like a parachute. It doesn’t work unless it’s open.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
The only attribute I tend to use is the
DescriptionAttribute
on an enum (like PIEBALDconsult's enum utils). In particular, I had to display a "meaningful" error message for about 30 different error codes, and it was convenient to define the codes as an enum with a human-readableDescriptionAttribute
I could display. I've also used this when I need to populate a ComboBox with the values of an enum. Other than that and some occasional designer attributes (Category
,Behavior
, etc. for the Property Page), I typically don't need to use attributes at all. DybsThe shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
I use attribute a lot with monorail to create custom data binding method (for instance I want to create my object using a constructor with parameters). But I think it's not a good idea to use it like in ActiveRecord, your mapping code is supposed to be apart from your class code.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
-
We're using PostSharp[^] too. I find it nice to be able to add one line of (attribute) code on top of a method, and have performance counters captured for that method. Open up PerfMon and see how may times my method is being hit. Attributes are a good way to abstract out the "non-method specific" functionality of a method. (I.e. If your method is called GetUser(), it's nice to be able to put performance tracing, error handling, entry/exit validation, etc., all without having to "spaghetti up" the code!) As far as reflection/performance, the good thing about PostSharp[^] is that the overhead is at compile time, not runtime!
-------------- Henry Manager, Software Engineering and Professional Angry Monkey Slayer