English 中文(简体)
Performance overhead of using attributes in .NET
原标题:

1.. Is there any performance overhead caused by the usage of attributes? Think for a class like:

    public class MyClass
    {
       int Count {get;set;}
    }

where it has 10 attibutes (attributes being classes, where the attribute classes themselves are way larger than MyClass itself like:

public class FirstAttribute : Attribute
{
   int A,B,C,D,E,F,G,H,I,J ... {get;set;}
}

2.. Would 10 of these attributes be a memory overhead everytime MyClass is instantiated? (FirstAttribute being 10 times the size of MyClass which will be decorated with 10 of these, so making the actual object itself so small compared to the total size of the attributes that are decorated on it.) Is this a concern?

3.. Would this scenario be any different for structs (Structs being value types and attributes being reference types)?

4.. Where are attibutes stored in memory in relation to the object that they are attached? How are they hooked together?

5.. Are attributes initialized as soon as MyClass is initialized or when you use reflection to retrieve them?

最佳回答
  1. There s a tiny bit of overhead in terms of space, but not a lot - attributes don t get in the way of normal execution.

  2. No, attributes act on types, not instances, so you won t take up a vast amount of memory by using lots of "big" attributes. (I don t know whether you get one per concrete type for generics, or one per generic type definition - I d expect the latter...)

  3. No, because of the answer to 1.

  4. Attributes aren t attached to objects - they re attached to types. I don t know the details of exactly where they re stored in memory, but that s an implementation detail anyway.

  5. Attributes are only initialized when you use reflection.

问题回答

Jon Skeet is absolute right and I only want to give one additional notion:

If you have a look at the base class of all attributes, System.Attribute, you ll notice, that most of its members are static. So they exist only once, no matter how many Attribute instances you have.

This is just another point to underline, that Attributes are not too costly...





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签