473,238 Members | 1,860 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,238 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 2817
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" ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.