Reflection Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm new to custom attributes. Here's the attribute I made:

Public Class DeveloperNoteAttribute
Inherits System.Attribute


Sub New(s as String)

End Sub
End Class

Notice that I don't hold onto the string that is passed in and that I have
no variables in the class. Despite this, the string parameter that I pass in
still gets stored in metadata. Why is this? What is the purpose, therefore,
in having underlying variables in the class itself? Do they serve any
purpose? Which data is decided to get written to metadata?

-Ben
 
Ben R. said:
I'm new to custom attributes. Here's the attribute I made:

Public Class DeveloperNoteAttribute
Inherits System.Attribute


Sub New(s as String)

End Sub
End Class

Notice that I don't hold onto the string that is passed in and that I have
no variables in the class. Despite this, the string parameter that I pass in
still gets stored in metadata. Why is this? What is the purpose, therefore,
in having underlying variables in the class itself? Do they serve any
purpose? Which data is decided to get written to metadata?

Anything that is specified in the attribute use is written to the
metadata, as when the class is loaded, that's how the attribute
instances are loaded.

The purpose of having underlying variables is so that whatever needs to
process the attribute (or whatever the attribute needs to do) gets to
use the data which is passed in the constructor.
 
Jon Skeet said:
Anything that is specified in the attribute use is written to the
metadata, as when the class is loaded, that's how the attribute
instances are loaded.

The purpose of having underlying variables is so that whatever needs to
process the attribute (or whatever the attribute needs to do) gets to
use the data which is passed in the constructor.

Hi Jon,

Could you elaborate a bit further? I didn't quite catch your explanation of
when underlying variables would come into play.

Also, your statement, "as when the class is loaded, that's how the attribute
instances are loaded." could you explain this a bit further as well? Thanks
for your time...

-Ben
 
Ben R. said:
Could you elaborate a bit further? I didn't quite catch your explanation of
when underlying variables would come into play.

Also, your statement, "as when the class is loaded, that's how the attribute
instances are loaded." could you explain this a bit further as well? Thanks
for your time...

When the class is loaded, the attributes you've specified are
instantiated using the parameters you've specified. Those attribute
instances can then be fetched using GetCustomAttributes - and that's
where the variables come in. Here's a simple sample program to
demonstrate:

using System;
using System.Collections;

[AttributeUsage(AttributeTargets.All)]
class SampleAttribute : Attribute
{
string name;
int score;

public SampleAttribute(string name)
{
this.name = name;
}

public int Score
{
get { return score; }
set { score = value; }
}

public string Name
{
get { return name; }
}
}

[Sample("Foo", Score=20)]
class Test
{
static void Main()
{
object[] attributes = typeof(Test).GetCustomAttributes
(typeof(SampleAttribute), false);

SampleAttribute attribute = (SampleAttribute)attributes[0];

Console.WriteLine ("Name={0}", attribute.Name);
Console.WriteLine ("Score={0}", attribute.Score);
}
}
 
Jon Skeet said:
Anything that is specified in the attribute use is written to the
metadata, as when the class is loaded, that's how the attribute
instances are loaded.

The purpose of having underlying variables is so that whatever needs to
process the attribute (or whatever the attribute needs to do) gets to
use the data which is passed in the constructor.

Hi Jon,

I thought I replied but it doesn't seem to have submitted properly. Could
you elaborate a bit further? Specifically:
"when the class is loaded, that's how the attribute instances are loaded."
I'm not sure I follow this.

Also:
"whatever needs to process the attribute (or whatever the attribute needs to
do) gets to use the data which is passed in the constructor."

Thanks for your time...

-Ben
 
Ben R. said:
I thought I replied but it doesn't seem to have submitted properly.

Yes it was - I've replied with a sample. Hopefully it'll have turned up
before this post does...
 
Back
Top