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. 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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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?
...
|
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...
|
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
...
|
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...
| |
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...
|
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.
...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |