473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reflection Question

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
Nov 22 '05 #1
10 1449
Ben R. <be**@newsgroup.nospam> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
Ben R. <be**@newsgroup.nospam> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #3
"Jon Skeet [C# MVP]" wrote:
Ben R. <be**@newsgroup.nospam> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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
Nov 22 '05 #4
"Jon Skeet [C# MVP]" wrote:
Ben R. <be**@newsgroup.nospam> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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
Nov 22 '05 #5
Ben R. <be**@newsgroup.nospam> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #6
Ben R. <be**@newsgroup.nospam> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #7
"Jon Skeet [C# MVP]" wrote:
Ben R. <be**@newsgroup.nospam> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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
Nov 22 '05 #8
"Jon Skeet [C# MVP]" wrote:
Ben R. <be**@newsgroup.nospam> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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
Nov 22 '05 #9
Ben R. <be**@newsgroup.nospam> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #10
Ben R. <be**@newsgroup.nospam> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
2
by: Dan | last post by:
Let's say I have a class like: class Dummy { public const string CONE = "one"; public const string CTWO = "two"; ... other stuff .... }
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
1
by: Pieter Breed | last post by:
Hi All, I am trying to use the attribute/reflection system to as much potential as I can think of, but I've run into a snag. I would appreciate it if someone would point me in the right...
5
by: heddy | last post by:
I understand that reflection allows me to discover the metadata of a class at runtime (properties, methods etc). What I don't understand is where this is useful. For example: If I am the sole...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, is it faster to access the fields from an object directly by using reflection than accessing them by their properties using reflection? Thanks Christian
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.