473,729 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Values of Unknown Custom Attributes

Hello All,

I am sure that I am just overlooking something, but here's something I can't
quite get right...

I want to be able to get the value of a parameter of an unknown custom
attribute at runtime. All of the examples I have seen use casting of known
custom attributes to get these values.

Thanks,
pagates
Mar 29 '07 #1
5 1873
pagates,

Custom attributes are classes, they are not methods with parameters.
Getting any information from them using reflection is like using reflection
to get information from any other class. You have to have some expectation
of what you are looking for.

Can you provide an example of what you are trying to do?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"pagates" <pa*****@discus sions.microsoft .comwrote in message
news:17******** *************** ***********@mic rosoft.com...
Hello All,

I am sure that I am just overlooking something, but here's something I
can't
quite get right...

I want to be able to get the value of a parameter of an unknown custom
attribute at runtime. All of the examples I have seen use casting of
known
custom attributes to get these values.

Thanks,
pagates

Mar 29 '07 #2
Hi Nicholas,

Sure - it's a general question, but I wasn't sure how to word it...
Say I have something like this:
public class Class1
{
public Class1()
{
}

string _someProperty;
[ACustomAttribut e("Some Custom Attribute Value")]
public string SomeProperty
{
get { return _someProperty; }
set { _someProperty = value; }
}
}

I simply want to use reflection to get "Some Custom Attribute Value" from
the ACustomAttribut e attribute in a program that loads the assembly that
contains Class1. I can use GetCustomAttrib utes to get the attribute itself,
but I can't quite get how to get the value of the parameter contained therein.

Does that make sense? I can see what I want in my head, but getting it out
of there might be a problem.... : )

Thanks,
pagates
"Nicholas Paldino [.NET/C# MVP]" wrote:
pagates,

Custom attributes are classes, they are not methods with parameters.
Getting any information from them using reflection is like using reflection
to get information from any other class. You have to have some expectation
of what you are looking for.

Can you provide an example of what you are trying to do?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"pagates" <pa*****@discus sions.microsoft .comwrote in message
news:17******** *************** ***********@mic rosoft.com...
Hello All,

I am sure that I am just overlooking something, but here's something I
can't
quite get right...

I want to be able to get the value of a parameter of an unknown custom
attribute at runtime. All of the examples I have seen use casting of
known
custom attributes to get these values.

Thanks,
pagates


Mar 29 '07 #3
pagates,

Ok, I understand now.

So, when you have the attribute like this:

[ACustomAttribut e("Some Custom Attribute Value")]
public string SomeProperty

The runtime constructs an instance of ACustomAttribut e passing the value
of "Some Custom Attribute Value". What the constructor does with this is up
to the attribute class. It's like any other class in this sense.
Typically, however, it will be exposed as a property. So, on the
ACustomAttribut e class, there should be some sort of property that exposes
the value. When you call GetCustomAttrib utes, you can cast the return value
to an instance of ACustomAttribut e and then access the property to get the
value (assuming it is exposed as such).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"pagates" <pa*****@discus sions.microsoft .comwrote in message
news:D5******** *************** ***********@mic rosoft.com...
Hi Nicholas,

Sure - it's a general question, but I wasn't sure how to word it...
Say I have something like this:
public class Class1
{
public Class1()
{
}

string _someProperty;
[ACustomAttribut e("Some Custom Attribute Value")]
public string SomeProperty
{
get { return _someProperty; }
set { _someProperty = value; }
}
}

I simply want to use reflection to get "Some Custom Attribute Value" from
the ACustomAttribut e attribute in a program that loads the assembly that
contains Class1. I can use GetCustomAttrib utes to get the attribute
itself,
but I can't quite get how to get the value of the parameter contained
therein.

Does that make sense? I can see what I want in my head, but getting it
out
of there might be a problem.... : )

Thanks,
pagates
"Nicholas Paldino [.NET/C# MVP]" wrote:
>pagates,

Custom attributes are classes, they are not methods with parameters.
Getting any information from them using reflection is like using
reflection
to get information from any other class. You have to have some
expectation
of what you are looking for.

Can you provide an example of what you are trying to do?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"pagates" <pa*****@discus sions.microsoft .comwrote in message
news:17******* *************** ************@mi crosoft.com...
Hello All,

I am sure that I am just overlooking something, but here's something I
can't
quite get right...

I want to be able to get the value of a parameter of an unknown custom
attribute at runtime. All of the examples I have seen use casting of
known
custom attributes to get these values.

Thanks,
pagates



Mar 29 '07 #4
This approach is only possible if you can actually cast to the
CustomAttribute . If I reflect on an Assembly that also declares the
CustomAttribute Type I will not be able to cast to this Attribute type
because I do not reference that assembly. How could this issue be
solved ?

On Mar 29, 6:01 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
pagates,

Ok, I understand now.

So, when you have theattributelik e this:

[ACustomAttribut e("Some CustomAttribute Value")]
public string SomeProperty

The runtime constructs an instance of ACustomAttribut e passing the value
of "Some CustomAttribute Value". What the constructor does with this is up
to theattributecla ss. It's like any other class in this sense.
Typically, however, it will be exposed as aproperty. So, on the
ACustomAttribut e class, there should be some sort ofpropertythat exposes
the value. When you call GetCustomAttrib utes, you can cast the return value
to an instance of ACustomAttribut e and then access thepropertytoge tthe
value (assuming it is exposed as such).

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard .caspershouse.c om

"pagates" <paga...@discus sions.microsoft .comwrote in message

news:D5******** *************** ***********@mic rosoft.com...
Hi Nicholas,
Sure - it's a general question, but I wasn't sure how to word it...
Say I have something like this:
public class Class1
{
public Class1()
{
}
string _someProperty;
[ACustomAttribut e("Some CustomAttribute Value")]
public string SomeProperty
{
get{ return _someProperty; }
set { _someProperty = value; }
}
}
I simply want to usereflectionto get"Some CustomAttribute Value" from
the ACustomAttribut eattributein a program that loads the assembly that
contains Class1. I can use GetCustomAttrib utes togettheattribu te
itself,
but I can't quitegethow togetthe value of the parameter contained
therein.
Does that make sense? I can see what I want in my head, but getting it
out
of there might be a problem.... : )
Thanks,
pagates
"Nicholas Paldino [.NET/C# MVP]" wrote:
pagates,
Custom attributes are classes, they are not methods with parameters.
Getting any information from them usingreflection is like using
reflection
togetinformatio n from any other class. You have to have some
expectation
of what you are looking for.
Can you provide an example of what you are trying to do?
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard .caspershouse.c om
"pagates" <paga...@discus sions.microsoft .comwrote in message
news:17******* *************** ************@mi crosoft.com...
Hello All,
I am sure that I am just overlooking something, but here's something I
can't
quitegetright.. .
I want to be able togetthe value of a parameter of an unknown custom
attributeat runtime. All of the examples I have seen use casting of
known
custom attributes togetthesevalue s.
Thanks,
pagates- Hide quoted text -

- Show quoted text -

May 14 '07 #5
Fist - you can't access *parameters*, but you can access *properties*;
and there-in lies the key.

If you don't want to reference the assembly, then perhaps use the
System.Componen tModel to access the values at runtime - i.e. (notepad
code; not compile-tested)

foreach(Attribu te untypedAttribut e in ...) { // presumably
GetAttributes() etc
Debug.WriteLine (untypedAttribu te.GetType().Na me);
foreach(Propert yDescriptor property in
TypeDescriptor. GetProperties(u ntypedAttribute )) {
Debug.WriteLine (string.Format( "{0}={1}", property.Name,
property.GetVal ue(untypedAttri bute)));
}
}

If you want to work with a *specific* attribute, then my first choice
would be to reference it! Otherwise, perhaps filter on FQN on
FullName.

Marc
May 14 '07 #6

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

Similar topics

8
2187
by: nicolas.sanguinetti | last post by:
Hi, I want to add custom attributes to my xhtml documents to use with my DOM scripts. For example, I want to have some tags -say, the <h1>- have an attribute and a . The thing is that I also want the document to validate against xhtml 1.1 :-) How can I define a Schema (and embed it in my xhtml) to expand XHTML default and add this custom attributes?
12
5800
by: Pol Bawin | last post by:
Hi All, Did somebody already define attributes for numerical properties to define value : minima, maxima, a number of decimal, ...? This information would be useful to unify syntax Polo
2
2383
by: Zach Mortensen | last post by:
I can't seem to get dynamically-compiled JScript code to use C#-defined custom attributes. I have a simple attribute and a class defined in a C# assembly: namespace MyNamespace { public abstract class MyCsharpAttribute : Attribute { }
1
2141
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 not consistent: (0x0c0001a5). Display.obj : error LNK2022: metadata operation failed (80131195) : Custom attributes are not consistent: (0x0c000108). The help for LNK2022 is completely useless:
4
2051
by: mark | last post by:
Could someone please give me a practical example of how you HAVE used custom attributes ... not just how you COULD use them. I understand the syntax, but am not able to visualize a good use for them. Thanks in advance. Mark
3
3195
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what the set piece of the Name property gets compiled to, which I am getting from the stack trace, then the attributes are not returned. For example, Class Person has a property called "Name" which has a custom attribute decorating it. Inside the set...
2
2527
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article is the usage of "Custom Attribute" in real life project. Can somebody please help me understand where are these informations really helpful in Development Environment; may be a few example(s) will help me understand.
4
1821
by: davearkley | last post by:
I've recently 'discover' the wonders of custom attributes and reflection. There's one aspect that has stumping me and I've been unable to find samples in the docs or on the web. I have fields in a class which have zero, one or more custom attributes associated with each field. I'd like to get a list of the attributes present for a given field, and then iterate over them to find associated values. Where I've coded for the presence of...
2
13885
by: P4trykx | last post by:
Hello I'm want to add some custom attributes to WebControls using WebControl.Attributes.Add("abc","234"); So the html output will look like this, <input type="hidden" abc="123" /etc. I know that I need to modify .dtd file and tell the browser that abc i legal attribute. But I have another problem, I can't change the abc attribute using
0
9280
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.