473,397 Members | 1,985 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,397 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 1451
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.