473,387 Members | 1,942 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,387 software developers and data experts.

Get the attribute of a class within this class

Hello all,

using System;

namespace Test
{
[AttributeUsage(AttributeTargets.Class,
AllowMultiple = false)]
public class FactoryAttribute : System.Attribute
{
public string Identifier { get { return id_; } }
private string id_;

public FactoryAttribute(string identifier)
{
id_ = identifier;
}
}

[FactoryAttribute("MaladieLundi")]
class Foo
{
}
}

Is it possible IN Foo to get the value of the identifier of
FactoryAttribute?

thanks in advance

Mike
Jun 27 '08 #1
3 1266
Yes,
do this in a method:
FactoryAttribute fa =
(FactoryAttribute)this.GetType().GetCustomAttribut es(typeof(FactoryAttribute), false);

be warned though that enumerating custom attributes is very processing
intensive. It is possibly the slowest reflection operation.

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Michael" wrote:
Hello all,

using System;

namespace Test
{
[AttributeUsage(AttributeTargets.Class,
AllowMultiple = false)]
public class FactoryAttribute : System.Attribute
{
public string Identifier { get { return id_; } }
private string id_;

public FactoryAttribute(string identifier)
{
id_ = identifier;
}
}

[FactoryAttribute("MaladieLundi")]
class Foo
{
}
}

Is it possible IN Foo to get the value of the identifier of
FactoryAttribute?

thanks in advance

Mike
Jun 27 '08 #2
Yes,
do this in a method:
FactoryAttribute fa =
(FactoryAttribute)this.GetType().GetCustomAttribut es(typeof(FactoryAttribute), false);

be warned though that enumerating custom attributes is very processing
intensive. It is possibly the slowest reflection operation.
OK, thank you for that answer.

And what is the way to do that from a static function?
Jun 27 '08 #3
On Apr 28, 2:51 pm, Michael <michael.de...@gmail.comwrote:
Yes,
do this in a method:
FactoryAttribute fa =
(FactoryAttribute)this.GetType().GetCustomAttribut es(typeof(FactoryAttribute), false);
be warned though that enumerating custom attributes is very processing
intensive. It is possibly the slowest reflection operation.

OK, thank you for that answer.

And what is the way to do that from a static function?
Use typeof(YourClassNameHere) instead of this.GetType().

Jon
Jun 27 '08 #4

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

Similar topics

4
by: python newbie | last post by:
Is there anything that would get in the way of me being able to reference an object attribute, from within a list. I have: # one class here class BackupSet: fileGroupList = # ...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
33
by: bissatch | last post by:
Hi, I fully understand the purpose of an alt attribute within a <img> tag but why would you use a title or summary attribute within, for example, a <p> tag. I have read books recommending that I...
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
0
by: Martin Aliger | last post by:
Hi all, is there any way how to access type which is decorated with mine attribute within that attribute? E.g. public class Whatever {
4
by: Dabbler | last post by:
I'm trying to block out some text by setting Label width attribute which renders as a span tag so that all labels for textfields take up the same amount of space hence the textfields are all...
4
by: hardieca | last post by:
Hi! I have built a custom attribute that tracks my list of things left to do in my application: Public Class SomeClass ... Using reflection, I am able to find all attributes within the...
4
by: yawnmoth | last post by:
Say I wanted to make a link change colors when someone was hovering over it. Ordinarily, I could do that by doing something like... <style> a { color: whatever; } a:hover { color:whatever;
0
by: pradip sonar | last post by:
Hello, I am having a problem with Xalan transforming a document containing Japanese characters. (An initial note: the problem does not occur on my Windows development machine, only on the...
2
by: scdowney | last post by:
First and foremost, thank you in advance for any attempts to help me out. I am working on a project with work, and it requires I use CSS selectors to locate elements within a webpage. For the...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.