Retrieving Custom Attributes from a property using StackTrace

G

Guest

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 brace in the property I want to
retrieve the custom attributes from the Property and do some processing (in
my real world code the attributes are to be used for validating input, i.e.
user length, max min values etc). I can do this as long as I pass the name
of the property i.e. "Name" to the GetMember method of the Type class.
However, if I try to get it using the name the Set property gets compile to,
set_Name which comes from the StackTrace no custom attributes are returned.

I could pass the name of the property to the method, but this seems to be
something that would be a source of bugs, if I change the property name and
forget to change the string value I am passing to the attribute processing
method.

Here is an example of the code (I appologise for the formatting in this
window):

using System;
using System.Collections.Generic;
using System.Text;

using System.Reflection;
using System.Diagnostics;

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Name = "Frank";
}
}

class CustomTestAttribute : Attribute
{
}

class Person
{
[CustomTest]
public string Name
{
get
{
return "Bob";
}
set
{
//THIS WORKS
ProcessAttributes("Name");

//THIS DOES NOT WORK, when passing set_Name
StackTrace stackTrace = new StackTrace();
ProcessAttributes(stackTrace.GetFrame(0).GetMethod().Name);
}
}

private void ProcessAttributes(string memberName)
{
//get the member that called this method
MemberInfo[] members = this.GetType().GetMember(
memberName, BindingFlags.Public | BindingFlags.Instance);

//Get the attributes
object[] attributes = members[0].GetCustomAttributes(false);

}
}
}


Thanks in advance
Mark.
 
B

Brian P

I'm no reflections expert, but this sounded interesting so I thought I
would poke around at it.

In my tinkering, I found that if you put the attributes at the "set"
level, you will get them in your ProcessAttributes method:


public string Name
{
get { return "Bob"; }

[CustomTest]
set
{
...
}

}


So, somehow to get the attributes of the Name property, you need to
somehow get out of the set section and up one level into the Name
property. But, I cannot find a link between the set_Name method and its
"parent property".

--Brian
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

You are going to have to parse off the set_ or the get_ part of the
method name and then get the PropertyInfo instance for the property. The
method doesn't have the attributes assigned to it, just the property.
Properties are always backed by methods (which is why there is a property to
get the method info for the method backing the property), but not the other
way.

What you could do is once you get the property, you can call the
GetGetMethod or GetSetMethod on the property that you retrieved. If it
matches the method that you started with, then you know it is the same.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mark R. Dawson said:
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 brace in the property I want to
retrieve the custom attributes from the Property and do some processing
(in
my real world code the attributes are to be used for validating input,
i.e.
user length, max min values etc). I can do this as long as I pass the
name
of the property i.e. "Name" to the GetMember method of the Type class.
However, if I try to get it using the name the Set property gets compile
to,
set_Name which comes from the StackTrace no custom attributes are
returned.

I could pass the name of the property to the method, but this seems to be
something that would be a source of bugs, if I change the property name
and
forget to change the string value I am passing to the attribute processing
method.

Here is an example of the code (I appologise for the formatting in this
window):

using System;
using System.Collections.Generic;
using System.Text;

using System.Reflection;
using System.Diagnostics;

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Name = "Frank";
}
}

class CustomTestAttribute : Attribute
{
}

class Person
{
[CustomTest]
public string Name
{
get
{
return "Bob";
}
set
{
//THIS WORKS
ProcessAttributes("Name");

//THIS DOES NOT WORK, when passing set_Name
StackTrace stackTrace = new StackTrace();
ProcessAttributes(stackTrace.GetFrame(0).GetMethod().Name);
}
}

private void ProcessAttributes(string memberName)
{
//get the member that called this method
MemberInfo[] members = this.GetType().GetMember(
memberName, BindingFlags.Public | BindingFlags.Instance);

//Get the attributes
object[] attributes = members[0].GetCustomAttributes(false);

}
}
}


Thanks in advance
Mark.
 
G

Guest

Hi Brian,
thanks for looking into this. Maybe you are onto something with moving
the attributes inside the set, I am going to look more into this to see if I
can find out if it can be used.

Thanks
Mark.
 

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