B
Bob Cohen \(106531\)
I want to define some metadata using custom attributes to apply to
properties in various classes.
The properties return values that are classes themselves.
I want to retrieve the attribute information in the classes from which the
fields/properties are derived, but I can't figure out how to get them. I
seem to be stuck since the attributes are declared against the property, but
the information is returned from the base class; I don't see how to get "the
current" attribute information without knowing the specific name of the
property ahead of time.
i.e. Attribute.GetCustomAttribute() or Attribute.GetCustomAttributes() both
want to know the MemberInfo of the property first, but you can't get the
MemberInfo without knowing the name of the property. I don't know how to
get the name of the calling property from within its class. And
this.GetType().GetCustomAttributes() returns information about the base
type, not the calling property.
Here's a simple app that demonstrations what I'd like to accomplish:
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DbIntField _xxx = new DbIntField();
[Test(true)]
public DbIntField xxx
{
get { return _xxx; }
}
}
[AttributeUsage(AttributeTargets.Property)]
public class TestAttribute : Attribute
{
public bool IsTestable = false;
public TestAttribute(bool IsTestable)
{
this.IsTestable = IsTestable;
}
}
public class DbIntField
{
public bool IsTestable
{
get
{
===> How to get to the test attribute without explicitly
specifying the string "xxx"???
}
}
}
}
Thanks,
Bob Cohen
properties in various classes.
The properties return values that are classes themselves.
I want to retrieve the attribute information in the classes from which the
fields/properties are derived, but I can't figure out how to get them. I
seem to be stuck since the attributes are declared against the property, but
the information is returned from the base class; I don't see how to get "the
current" attribute information without knowing the specific name of the
property ahead of time.
i.e. Attribute.GetCustomAttribute() or Attribute.GetCustomAttributes() both
want to know the MemberInfo of the property first, but you can't get the
MemberInfo without knowing the name of the property. I don't know how to
get the name of the calling property from within its class. And
this.GetType().GetCustomAttributes() returns information about the base
type, not the calling property.
Here's a simple app that demonstrations what I'd like to accomplish:
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DbIntField _xxx = new DbIntField();
[Test(true)]
public DbIntField xxx
{
get { return _xxx; }
}
}
[AttributeUsage(AttributeTargets.Property)]
public class TestAttribute : Attribute
{
public bool IsTestable = false;
public TestAttribute(bool IsTestable)
{
this.IsTestable = IsTestable;
}
}
public class DbIntField
{
public bool IsTestable
{
get
{
===> How to get to the test attribute without explicitly
specifying the string "xxx"???
}
}
}
}
Thanks,
Bob Cohen