473,498 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3160

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5478
by: Chris Newby | last post by:
I am trying to implment some business level user authorization in my current ..net 1.1 app. In C#, I would like to do something like: public void MethodRequiringAuthorization() { ... some...
2
1970
by: Rob | last post by:
What VB run-time methods are available to retrieve information (such as AssemblyCopyright, AssemblyProduct, AssemblyVersion, etc.) from the AssemblyInfo.vb file for the current executable? ...
2
3168
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid...
3
1485
by: Gigi | last post by:
I'm trying to add a custom property to each row of a table. So I created an HtmlTable called tblClass, I created a new class myRow inherited from HtmlTableRow, to which I added a new member myRow ...
0
1027
by: Sanjay Pais | last post by:
This is an extension of an earlier post I have created a custom text box and compiled it and dropped it into my toolbox. However when I change the value of my custom property in design mode and...
1
1688
by: Sanjay Pais | last post by:
I built a custom control for all the basic web.ui.controls like textbox, label, checkbox etc etc. I added my custom attribute called ApplySecurity to the html in the page. However, when I cycle...
6
1563
by: Steve Amey | last post by:
Hi all I want to be able to throw a custom error up the call stack. I have looked around and it seems as though it's possible, but I can't get it to work :o( Below is some sample code. ...
5
1856
by: =?Utf-8?B?cGFnYXRlcw==?= | last post by:
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...
3
1775
by: Michel Vanderbeke | last post by:
Hello, While logging the errors in my program, I want to know in which class, function or procedure they occured. Is it possible to know the name of the class, function and / or procedure in...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.