473,408 Members | 2,832 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,408 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".
}
}
}
Nov 16 '05 #1
4 1317
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".
}
}
}

Nov 16 '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".
}
}
}


Nov 16 '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".
}
}
}



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

Paul
Nov 16 '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: ...
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: 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...
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...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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,...

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.