Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Using Attributes

Using Attributes

Scheduled Pinned Locked Moved The Lounge
questioncsharpdesignperformancetutorial
24 Posts 23 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Richard Blythe
    wrote on last edited by
    #1

    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.

    P H R E D 21 Replies Last reply
    0
    • R Richard Blythe

      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.

      P Offline
      P Offline
      peterchen
      wrote on last edited by
      #2

      Serialization that's not broken, convoluted and a pain?

      Agh! Reality! My Archnemesis![^]
      | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.

      1 Reply Last reply
      0
      • R Richard Blythe

        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.

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #3

        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.'

        1 Reply Last reply
        0
        • R Richard Blythe

          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.

          R Offline
          R Offline
          RugbyLeague
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • R Richard Blythe

            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.

            D Offline
            D Offline
            dan sh
            wrote on last edited by
            #5

            WCF will be nothing if everyone hates attributes and P/Invoke would be a bit angry too. :)

            1 Reply Last reply
            0
            • R Richard Blythe

              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.

              E Offline
              E Offline
              Ennis Ray Lynch Jr
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • R Richard Blythe

                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.

                H Offline
                H Offline
                hairy_hats
                wrote on last edited by
                #7

                Marshal(l)ing?

                1 Reply Last reply
                0
                • R Richard Blythe

                  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.

                  K Offline
                  K Offline
                  Keith Barrow
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • R Richard Blythe

                    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 Offline
                    I Offline
                    Ian Shlasko
                    wrote on last edited by
                    #9

                    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)

                    1 Reply Last reply
                    0
                    • R Richard Blythe

                      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.

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • R Richard Blythe

                        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.

                        J Offline
                        J Offline
                        Joe Woodbury
                        wrote on last edited by
                        #11

                        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.)

                        1 Reply Last reply
                        0
                        • R Richard Blythe

                          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.

                          S Offline
                          S Offline
                          Single Step Debugger
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • R Richard Blythe

                            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.

                            D Offline
                            D Offline
                            Dan Neely
                            wrote on last edited by
                            #13

                            Besides what's been listed by peterchen and Henry Minute above IIRC the only thing I've used them for was when I needed to change an enum from default behavior (0,1,2,3,4...) into bitflags (0,1,2,4,8...).

                            3x12=36 2x12=24 1x12=12 0x12=18

                            1 Reply Last reply
                            0
                            • R Richard Blythe

                              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.

                              M Offline
                              M Offline
                              Marc Clifton
                              wrote on last edited by
                              #14

                              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

                              R 1 Reply Last reply
                              0
                              • M Marc Clifton

                                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

                                R Offline
                                R Offline
                                Richard Blythe
                                wrote on last edited by
                                #15

                                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.

                                1 Reply Last reply
                                0
                                • R Richard Blythe

                                  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.

                                  D Offline
                                  D Offline
                                  dybs
                                  wrote on last edited by
                                  #16

                                  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-readable DescriptionAttribute 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. Dybs

                                  The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

                                  1 Reply Last reply
                                  0
                                  • R Richard Blythe

                                    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.

                                    D Offline
                                    D Offline
                                    Donsw
                                    wrote on last edited by
                                    #17

                                    Depends on the location. If I need the speed no, but there are many places where speed of the application is not a concern.

                                    cheers, Donsw My Recent Article : CDC - Change Data Capture

                                    1 Reply Last reply
                                    0
                                    • R Richard Blythe

                                      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.

                                      R Offline
                                      R Offline
                                      Remi BOURGAREL
                                      wrote on last edited by
                                      #18

                                      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.

                                      1 Reply Last reply
                                      0
                                      • R Richard Blythe

                                        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.

                                        J Offline
                                        J Offline
                                        Jacquers
                                        wrote on last edited by
                                        #19

                                        I've used them with PostSharp[^] which I found very useful.

                                        H 1 Reply Last reply
                                        0
                                        • J Jacquers

                                          I've used them with PostSharp[^] which I found very useful.

                                          H Offline
                                          H Offline
                                          HenryChilvers
                                          wrote on last edited by
                                          #20

                                          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

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups