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 921057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:48:52+00:00 2026-05-15T18:48:52+00:00

Say I have this class: [AttributeUsage(AttributeTargets.Method)] public class MyAttribute : Attribute { public MyAttribute()

  • 0

Say I have this class:

[AttributeUsage(AttributeTargets.Method)] 
public class MyAttribute : Attribute 
{ 
  public MyAttribute() 
  { 
    // Do stuff
  }

  ~MyAttribute()
  {
    // When is this called? When the function ends? Whenever the GC feels like?
  }
}
  • 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. Editorial Team
    Editorial Team
    2026-05-15T18:48:52+00:00Added an answer on May 15, 2026 at 6:48 pm

    Following an example call of GetCustomAttributes through in Reflector, the managed part of the code (i.e. the point at which it transitions to the runtime and becomes an external call) is down in CustomAttribute.GetCustomAttributes.

    At this point, the method is examining the bytes of the metadata surrounding the object for which the attributes are being loaded.

    There is code in there which then does further reflection to find the runtime constructor being called. E.g.

    [MyAttribute]
    

    Would be calling the default, whereas

    [MyAttribute(1, "hello", typeof(T))]
    

    Would be calling a constructor that takes (Int, String, Type).

    I can’t see any evidence in there that any caching of instances is performed which therefore means that attribute instances are created on demand when they are reflected.

    Proof

    The aforementioned managed-runtime transition happends at CustomAttribute._CreateCaObject. Whilst not easy to statically analyse whether this method does in fact cache the instances it creates (it does potentially get enough state information in the form of a memory buffer pointer presumably indicating the location in metadata where the attribute declaration resides) we can look at the facts:

    • The constructor is always called, and
    • New constructor parameters are always read and fed in.

    This tells me that the attribute is always constructed.

    We can test for this, of course, by writing a piece of code in a test.

    [TestMethod]
    public void TestMethod1()
    {
      //if running in MSTest you have to allow for the test runner to reflect 
      //over the class as it looks for the TestClass attribute - therefore if our
      //assumption is correct that a new instance is always constructed when 
      //reflecting, our counter check should start at 2, not 1.
      Type t = typeof(AttributeTest);
      var attributes = 
        t.GetCustomAttributes(typeof(AttributeTest.TheAttributeAttribute), false);  
      //check counter
      Assert.AreEqual(2, AttributeTest.TheAttributeAttribute.Counter);
      var attributes2 = 
        t.GetCustomAttributes(typeof(AttributeTest.TheAttributeAttribute), false);
      //should be one louder (sorry, 'one bigger' - the Spinal Tap influence :) )
      Assert.AreEqual(3, AttributeTest.TheAttributeAttribute.Counter);
    }
    
    [TheAttribute]
    public class AttributeTest
    {
      public class TheAttributeAttribute : Attribute
      {
        static int _counter = 0;
    
        public static int Counter { get { return _counter; } }
    
        public TheAttributeAttribute()
        {
          _counter++;
          Console.WriteLine("New");
        }
      }
    }
    

    Therefore an efficient use of metadata attributes would be to cache them in user code, unless of course the attribute is mutable in some way that makes it not applicable for all instances of a given T, or all ‘instances’ (in quotes because of course a method is only stored in memory once) of a method m for instances of type T).

    Following this, therefore, an attribute is available to the GC once all references to it in code have been nulled. The same is true for all members of that attribute as well.

    Therefore a method which uses GetCustomAttributes() to retrieve an attribute, uses it and then throws away the reference has just released a new instance of that attribute for the GC to clean up when it needs to.

    Therefore – attribute instances are governed by the exact same memory management and lifetime rules as all class instances; therefore what @PieterG says in his answer is correct – the destructor could be called at any time after all references to that attribute have been released.

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

Sidebar

Ask A Question

Stats

  • Questions 505k
  • Answers 505k
  • 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
  • Editorial Team
    Editorial Team added an answer suppose you have the following form : <form id="ajax-form"> <fieldset>… May 16, 2026 at 3:28 pm
  • Editorial Team
    Editorial Team added an answer I think it's a better idea to use "optional" fields.… May 16, 2026 at 3:28 pm
  • Editorial Team
    Editorial Team added an answer Well, it's possible over one connection (Well, one connection from… May 16, 2026 at 3:28 pm

Trending Tags

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

Top Members

Related Questions

Say I have this class Myclass that contains this method: public class MyClass {
Say I have this class : public class BaseJob{ String name; public void setName(String
Say I have this class: class myclass { public int Field1{ get; set; }
Say I have this class: public class Account { public int AccountID { get;
say I have this class: class animal { function noise() { print 'woof'; }
say I have this control: public partial class bloc999 : UserControl { bloc999Data mainBlock
Lets say have this immutable record type: public class Record { public Record(int x,
let's say i have this public class MyClass { public string myMessage { get;
Lets say i have this entity public class Address : Entity { public Address()
Say I have a component like this public class MyComponent { public MyComponent(string name)

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.