473,408 Members | 2,734 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.

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 1853
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.com

"pagates" <pa*****@discussions.microsoft.comwrote in message
news:17**********************************@microsof t.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;
[ACustomAttribute("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 ACustomAttribute attribute in a program that loads the assembly that
contains Class1. I can use GetCustomAttributes 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.com

"pagates" <pa*****@discussions.microsoft.comwrote in message
news:17**********************************@microsof t.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:

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

The runtime constructs an instance of ACustomAttribute 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
ACustomAttribute class, there should be some sort of property that exposes
the value. When you call GetCustomAttributes, you can cast the return value
to an instance of ACustomAttribute and then access the property to get the
value (assuming it is exposed as such).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"pagates" <pa*****@discussions.microsoft.comwrote in message
news:D5**********************************@microsof t.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;
[ACustomAttribute("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 ACustomAttribute attribute in a program that loads the assembly that
contains Class1. I can use GetCustomAttributes 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.com

"pagates" <pa*****@discussions.microsoft.comwrote in message
news:17**********************************@microso ft.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.guard.caspershouse.comwrote:
pagates,

Ok, I understand now.

So, when you have theattributelike this:

[ACustomAttribute("Some CustomAttributeValue")]
public string SomeProperty

The runtime constructs an instance of ACustomAttribute passing the value
of "Some CustomAttributeValue". What the constructor does with this is up
to theattributeclass. It's like any other class in this sense.
Typically, however, it will be exposed as aproperty. So, on the
ACustomAttribute class, there should be some sort ofpropertythat exposes
the value. When you call GetCustomAttributes, you can cast the return value
to an instance of ACustomAttribute and then access thepropertytogetthe
value (assuming it is exposed as such).

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

"pagates" <paga...@discussions.microsoft.comwrote in message

news:D5**********************************@microsof t.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;
[ACustomAttribute("Some CustomAttributeValue")]
public string SomeProperty
{
get{ return _someProperty; }
set { _someProperty = value; }
}
}
I simply want to usereflectiontoget"Some CustomAttributeValue" from
the ACustomAttributeattributein a program that loads the assembly that
contains Class1. I can use GetCustomAttributes togettheattribute
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 usingreflectionis like using
reflection
togetinformation 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.com
"pagates" <paga...@discussions.microsoft.comwrote in message
news:17**********************************@microso ft.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 togetthesevalues.
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.ComponentModel to access the values at runtime - i.e. (notepad
code; not compile-tested)

foreach(Attribute untypedAttribute in ...) { // presumably
GetAttributes() etc
Debug.WriteLine(untypedAttribute.GetType().Name);
foreach(PropertyDescriptor property in
TypeDescriptor.GetProperties(untypedAttribute)) {
Debug.WriteLine(string.Format("{0}={1}", property.Name,
property.GetValue(untypedAttribute)));
}
}

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
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...
12
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
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...
1
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...
4
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...
3
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...
2
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...
4
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...
2
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...
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
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...
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
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
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.