473,406 Members | 2,259 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,406 software developers and data experts.

How to retrieve Custom Attributes from the Type

Hi!
I have a problem:
I've declared some classes and decorated them with my Custom Attributes.
(say ClassA and ClassB)
Now I have some Type member (in fact this is FieldInfo.FieldType) which is
eq. typeof (ClassB).
And when I call GetCustomAttributes() for this Type I never gets the
attribute, that should be there.
When I call GetType().GetCustonAttributes() everything works fine.
Doeas object of the requested type for attributes must be instantiated ?

Thanks for any help.
Paul.

___ code for my example __

[MyAttribute()]
public class ClassA
{
public int iVal;
}

[MyOtherAttribute()]
public class ClassB
{

ClassA member ;

void foo()
{
foreach (MyAttribute arre in GetType().GetField
("member").GetCustomAttributes (typeof(MyAttribute), true))
{
// never enters here
// this loop never finds MyAttribute of ClassA of field
"member".
}
}
}
Jul 21 '05 #1
4 1491
The attribute in this case is applied to Class A. You don't have any
attributes applied to "member" and that's why it's not going in there.

You probably want to access the FieldType property of the member, eg.

foreach (MyAttribute arre in GetType().GetField
("member").FieldType.GetCustomAttributes (typeof(MyAttribute), true))
...

Hope that helps.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Paul" <no*****@dupa.com> wrote in message
news:eO**************@TK2MSFTNGP11.phx.gbl...
Hi!
I have a problem:
I've declared some classes and decorated them with my Custom Attributes.
(say ClassA and ClassB)
Now I have some Type member (in fact this is FieldInfo.FieldType) which is
eq. typeof (ClassB).
And when I call GetCustomAttributes() for this Type I never gets the
attribute, that should be there.
When I call GetType().GetCustonAttributes() everything works fine.
Doeas object of the requested type for attributes must be instantiated ?

Thanks for any help.
Paul.

___ code for my example __

[MyAttribute()]
public class ClassA
{
public int iVal;
}

[MyOtherAttribute()]
public class ClassB
{

ClassA member ;

void foo()
{
foreach (MyAttribute arre in GetType().GetField
("member").GetCustomAttributes (typeof(MyAttribute), true))
{
// never enters here
// this loop never finds MyAttribute of ClassA of field
"member".
}
}
}

Jul 21 '05 #2
Righ. I ommited by mistake those FieldType property. But this is the case,
when I do not get access to the atribute.
I check now, that when this field is null, GetCustomAttributes() throws
NullReferenceException.
But I do not want to extract attributes from the instance of the object, but
from the type info (System.Type).

Is it possible ? How to do this ?

grx
Paul
"John Wood" <j@ro.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
The attribute in this case is applied to Class A. You don't have any
attributes applied to "member" and that's why it's not going in there.

You probably want to access the FieldType property of the member, eg.

foreach (MyAttribute arre in GetType().GetField
("member").FieldType.GetCustomAttributes (typeof(MyAttribute), true))
...

Hope that helps.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Paul" <no*****@dupa.com> wrote in message
news:eO**************@TK2MSFTNGP11.phx.gbl...
Hi!
I have a problem:
I've declared some classes and decorated them with my Custom Attributes.
(say ClassA and ClassB)
Now I have some Type member (in fact this is FieldInfo.FieldType) which
is
eq. typeof (ClassB).
And when I call GetCustomAttributes() for this Type I never gets the
attribute, that should be there.
When I call GetType().GetCustonAttributes() everything works fine.
Doeas object of the requested type for attributes must be instantiated ?

Thanks for any help.
Paul.

___ code for my example __

[MyAttribute()]
public class ClassA
{
public int iVal;
}

[MyOtherAttribute()]
public class ClassB
{

ClassA member ;

void foo()
{
foreach (MyAttribute arre in GetType().GetField
("member").GetCustomAttributes (typeof(MyAttribute), true))
{
// never enters here
// this loop never finds MyAttribute of ClassA of field
"member".
}
}
}


Jul 21 '05 #3
I can't see why it wouldn't work... as soon as you use GetType() you're
working with the reflected types, not the instances, so that isn't the
problem.

Could you provide a full example, along with the error and stack trace.

Thanks.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Paul" <no*****@dupa.com> wrote in message
news:ec*************@TK2MSFTNGP10.phx.gbl...
Righ. I ommited by mistake those FieldType property. But this is the case,
when I do not get access to the atribute.
I check now, that when this field is null, GetCustomAttributes() throws
NullReferenceException.
But I do not want to extract attributes from the instance of the object, but from the type info (System.Type).

Is it possible ? How to do this ?

grx
Paul
"John Wood" <j@ro.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
The attribute in this case is applied to Class A. You don't have any
attributes applied to "member" and that's why it's not going in there.

You probably want to access the FieldType property of the member, eg.

foreach (MyAttribute arre in GetType().GetField
("member").FieldType.GetCustomAttributes (typeof(MyAttribute), true))
...

Hope that helps.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Paul" <no*****@dupa.com> wrote in message
news:eO**************@TK2MSFTNGP11.phx.gbl...
Hi!
I have a problem:
I've declared some classes and decorated them with my Custom Attributes. (say ClassA and ClassB)
Now I have some Type member (in fact this is FieldInfo.FieldType) which
is
eq. typeof (ClassB).
And when I call GetCustomAttributes() for this Type I never gets the
attribute, that should be there.
When I call GetType().GetCustonAttributes() everything works fine.
Doeas object of the requested type for attributes must be instantiated ?
Thanks for any help.
Paul.

___ code for my example __

[MyAttribute()]
public class ClassA
{
public int iVal;
}

[MyOtherAttribute()]
public class ClassB
{

ClassA member ;

void foo()
{
foreach (MyAttribute arre in GetType().GetField
("member").GetCustomAttributes (typeof(MyAttribute), true))
{
// never enters here
// this loop never finds MyAttribute of ClassA of field
"member".
}
}
}



Jul 21 '05 #4
I am so shame ;)
Everything works fine, it was all my mistake within the code.
Sorry for disturbing you, friedns.

Paul
Jul 21 '05 #5

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

Similar topics

5
by: Doug Holland | last post by:
Often you see code where an empty interface is used to indicate something about the class that realizes it. In the .NET world this can be done with custom attributes too, so which is better: ...
3
by: xAvailx | last post by:
I apologize ahead of time for the long post... Background: Working on a CRM type custom application. The application is for an event management company. The company will provide the application...
4
by: Wiktor Zychla | last post by:
Hello, for some reason I need to use attributes in ILAsm code and read these attributes in C# code. My problem is with the attribute declaration inside ILAsm code. I know that I define any...
4
by: Paul | last post by:
Hi! I have a problem: I've declared some classes and decorated them with my Custom Attributes. (say ClassA and ClassB) Now I have some Type member (in fact this is FieldInfo.FieldType) which is...
3
by: Edward Diener | last post by:
I understand the syntax of custom attributes, but I have no idea what they are supposed to do. Anyone care to give me a clue as to their functionality ?
6
by: Michael | last post by:
Scenario: I have a base class that I want to enumerate the custom attributes of a field defined in the child of the class. Is this possible? I've tried various versions of BindingFlags on...
16
by: Bret Pehrson | last post by:
I've converted a non-trivial C++ library to managed, and get the following unhelpful linker error: Assignment.obj : error LNK2022: metadata operation failed (80131195) : Custom attributes are...
0
by: george_Martinho | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older than a...
9
by: wardy1975 | last post by:
Hi All, Looking for a little expert advice on a few web standards issues. I am currently trying to understand the impact of web standards for a web application I work with. I have been doing a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.