Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 106865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:39:50+00:00 2026-05-11T01:39:50+00:00

So I was playing around a little more with attributes in .NET, and realized

  • 0

So I was playing around a little more with attributes in .NET, and realized that every call to Type.GetCustomAttributes() creates a new instance of my attribute. Why is that? I would think that the attribute instance would basically be a singleton-per-MemberInfo, with 1 instance bound to the Type, PropertyInfo, etc…

Here is my test code:

using System;  namespace AttribTest { [AttributeUsage(AttributeTargets.Class)] class MyAttribAttribute : Attribute {     public string Value { get; set; }      public MyAttribAttribute()         : base()     {         Console.WriteLine('Created MyAttrib instance');     } }  [MyAttrib(Value = 'SetOnClass')] class MyClass { }  class Program {     static void Main(string[] args)     {         Console.WriteLine('Getting attributes for MyClass.');         object[] a = typeof(MyClass).GetCustomAttributes(false);         ((MyAttribAttribute)a[0]).Value = 'a1';          Console.WriteLine('Getting attributes for MyClass.');         a = typeof(MyClass).GetCustomAttributes(false);         Console.WriteLine(((MyAttribAttribute)a[0]).Value);          Console.ReadKey();     } } } 

Now if I were to implement attributes, I would expect the output to be:

Created MyAttrib instance Getting attributes for MyClass. Getting attributes for MyClass. a1 

Where the ‘class loader’ (sorry, I have more of a Java background, not 100% sure how .net loads its types) would compile MyClass, and create an instance of MyAttribAttribute, and store them together somewhere. (probably the Perm Gen in the heap if this were Java) The 2 calls to GetCustomAttributes() would then just return the same earlier created instance.

But the actual output is:

Getting attributes for MyClass. Created MyAttrib instance Getting attributes for MyClass. Created MyAttrib instance SetOnClass 

So… why? It seems like creating a new instance of all these objects for every call is a bit excessive, and not good for performance/memory management. Is there any way to always get the same instance over and over?

Anyone have any ideas why it was designed this way?

The reason I care at all is because I made a custom attribute that internally holds some validation information, so in the Attribute I basically have a ‘private bool Validated’ that I set to true. The validation stuff takes a while, so I don’t want to run it every time. Now the problem is that since it creates a new instance of the attribute each time I get the attributes, Validated is always ‘false’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:39:51+00:00Added an answer on May 11, 2026 at 1:39 am

    Object-creation is cheap.

    If you had an attribute like

    public class MyAttribute : Attribute {     public virtual string MyText { get; set; } } 

    and applied it to a class like

    [MyAttribute(MyText='some text')] public class MyClass { } 

    and you retrieved one like

    var attr =     typeof(MyClass).GetCustomAttributes(typeof(MyAttribute), false)     .Cast<MyAttribute>().Single(); 

    and you set some properties on it like

    attr.MyText = 'not the text we started with'; 

    what should happen, and what would happen, the next time you called

    Console.WriteLine(     typeof(MyClass).GetCustomAttributes(typeof(MyAttribute), false)     .Cast<MyAttribute>().Single().Name ); 

    ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer It's because GLDrawable actually doesn't have such a method -… May 11, 2026 at 11:29 am
  • added an answer First get a server and figure out what kind of… May 11, 2026 at 11:29 am
  • added an answer It's open source and available on project's page, via BitBucket… May 11, 2026 at 11:29 am

Related Questions

So I was playing around a little more with attributes in .NET, and realized
So I was playing around the other day just to see exactly how mass
So I was listening to the latest Stackoverflow podcast ( episode 19 ), and
So I was writing some code today that basically looks like this: string returnString
So I was wondering if there are any major differences between the various implementations
So I was making a class the other day and used Eclipse's method to
So I was thinking about creating a dynamic sql question, meaning that i want
So I was thinking one way that you can bring method chaining into PHP
So I've looked at this use of the freebase API and I was really
So I need to track changes that happen on a Mysql table. I was

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.