472,354 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 1785
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.