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.
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.