6

I'm going over some c# tutorial that states the following:

"After it finds the attribute class, the compiler will check whether the attribute is allowed on a class. Then it will check for a constructor that matches the parameters specified in the attribute use. If it finds one, it creates an instance of the object by calling the constructor with the specified values"

The name of this paragraph is "compilation process", and it refers to the way the compiler handles attributes. I'm sorry for the ignorance, but shouldn't instances be created at run time?

Thanks.

2
  • 2
    Searching for that quote returns exactly 1 result: this question. Where did you get it from? Commented Nov 21, 2010 at 16:07
  • It sounds like compiler here is being used as a synonym for "code the compiler generates". Commented Nov 21, 2010 at 17:38

4 Answers 4

3

Attributes define metadata for classes -- every instance of the class (i.e. object) will share the same attribute values. This metadata is stored alongside the type definition, which is calculated once at compile time from the source files.

Sign up to request clarification or add additional context in comments.

Comments

2

Since there are attributes that also affect the compiler, I wouldn't be surprised if the compiler did instantiate them when compiling. So what?

6 Comments

I didn't know that the compiler instantiates objects.
Sounds rather unlikely. I doubt that the compiler instantiates attributes(i.e. it calls the constructor of a user defined attribute).
how would a compiler instantiate an object??
@miguel - compile the class, then fire up .NET and instantiate it? O_o
that is not the compiler instantiating an object. that is the runtime.
|
1

There is a "compilation" process that generates an intermediary code which is interpreted using .NET framework.

That's why they are talking about compilation process.

Comments

0

The compiler only creates metadata which describes the attribute construction and assignments to its properties. The attribute itself is only instantiated at the time you use reflection to list attributes. I think each call to reflection creates a new instance of that attribute.

And I think there is a way(related to reflection only contexts) to check the attribute directly from the meta without instantiating it at all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.