469,953 Members | 2,403 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

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 1711
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by nicolas.sanguinetti | last post: by
1 post views Thread by Bret Pehrson | last post: by
4 posts views Thread by mark | last post: by
2 posts views Thread by prabhupr | last post: by
4 posts views Thread by davearkley | last post: by
2 posts views Thread by P4trykx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.