471,605 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

Retrieving Custom Attributes from a property using StackTrace

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.
Feb 10 '06 #1
3 2946

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
Feb 10 '06 #2
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]
- mv*@spam.guard.caspershouse.com

"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
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.

Feb 10 '06 #3
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.
"Brian P" wrote:

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

Feb 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Chris Newby | last post: by
3 posts views Thread by Gigi | last post: by
1 post views Thread by Sanjay Pais | last post: by
6 posts views Thread by Steve Amey | last post: by
5 posts views Thread by =?Utf-8?B?cGFnYXRlcw==?= | last post: by

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.