473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How To Get Attributes From The "Current" Property???

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

May 20 '07 #1
3 2830
Well, the instance of a DbIntField isn't techically associated with
any specific property, not least since the same reference could
theoretically be associated from several properties (either as
separate "real" properties, or as pass-thru facades). As such, perhaps
your best approach is to work with the property rather than the
instance. In particular, this scenario seems like it might fit a
simple ITypeDescriptorContext implementation (i.e. public static bool
IsTestable(ITypeDescriptorContext ctx), which would provide the
property (PropertyDescriptor) and the owing instance, which infer the
value (the DbIntField) - or the value could be passed in separately.
This approach will work very will if you are already using
PropertyDescriptor view-based (rather than model-based) code.

The only other viable route involves intentionally making each
DbIntField aware of its context, perhaps via a parameterised ctor.
This is unwieldy at best, but can be made to work.

Marc

May 20 '07 #2
OK, I'll look into this. Although at first blush neither approach is
"attractive" for the coding model that I'm using. I'm just surprised that
the chain of ownership for this instance of the DbIntField isn't available
somewhere.

Thanks,
Bob

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@y18g2000prd.googlegr oups.com...
Well, the instance of a DbIntField isn't techically associated with
any specific property, not least since the same reference could
theoretically be associated from several properties (either as
separate "real" properties, or as pass-thru facades). As such, perhaps
your best approach is to work with the property rather than the
instance. In particular, this scenario seems like it might fit a
simple ITypeDescriptorContext implementation (i.e. public static bool
IsTestable(ITypeDescriptorContext ctx), which would provide the
property (PropertyDescriptor) and the owing instance, which infer the
value (the DbIntField) - or the value could be passed in separately.
This approach will work very will if you are already using
PropertyDescriptor view-based (rather than model-based) code.

The only other viable route involves intentionally making each
DbIntField aware of its context, perhaps via a parameterised ctor.
This is unwieldy at best, but can be made to work.

Marc
May 21 '07 #3
Again, see my point that in general the same (reference-type) object
could have multiple owners, without any precedence.

Marc
May 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Don G | last post by:
Within the menu for my site, I have inserted 'class="current"' within the link for the current page. So the link looks somthing link: <li><a href="index.php" class="current">Home</a></li> The...
4
by: Davids | last post by:
darn stuck again! in my string array string myArray = string; Let's say I've put a string field into the first 3 items and I guess the other 23 must then be null, right? OK so is there any C#...
1
by: VB Programmer | last post by:
When my users login I want them to be able to change some of their personal info in a db. I want to use a detailsview control for this. How can I get the detailsview to only pull up the record...
3
by: RR | last post by:
I have a button on a form (form A) that opens another form. The form that opens (form B) has a listbox that is populated with a call to a function in the "on current" event. When form B with the...
2
by: MLH | last post by:
Suppose that code running on FormB is moving the focus around on FormA to various textbox controls on FormA - Which form is the current form during this process? Is it FormA, which has the...
1
by: Peter Knörrich | last post by:
Hello, I've found another inconsistency, and looking through the list archives I can find mentions of funky stuff like print float('inf') giving Infanity
4
by: pamelafluente | last post by:
Hello. A question. It is possible to refer to the ID of an element within the element itself? And, if yes, what is the instruction or function. Example. Assume I have this: <div id="Square1" ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.