Values of Unknown Custom Attributes

G

Guest

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
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
G

Guest

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 said:
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]
- (e-mail address removed)

pagates said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

pagates said:
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 said:
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]
- (e-mail address removed)

pagates said:
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
 
B

bonk

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 ?

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]
- (e-mail address removed)




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

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]
- (e-mail address removed)
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 -
 
M

Marc Gravell

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top