473,503 Members | 722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

querying for properties that has implemented an attribute ?

KK
Hi,

I apply a custom Attribute to several properties
in a class. There are about 15 of those. I want
to know how I can programatically get ONLY those
properties that I have applied my attribute.

What I want is rather than calling those 15 attributes
like;

Object.Prop1
Object.Prop2
Object.Prop3 etc..

Is there a way to call it using reflection

foreach (Property that implements MyCustomAttribute){
object.Call Property that implements attribute
}

Thanks
KK
Dec 17 '05 #1
1 942
KK wrote:
Hi,

I apply a custom Attribute to several properties
in a class. There are about 15 of those. I want
to know how I can programatically get ONLY those
properties that I have applied my attribute.

What I want is rather than calling those 15 attributes
like;

Object.Prop1
Object.Prop2
Object.Prop3 etc..

Is there a way to call it using reflection

foreach (Property that implements MyCustomAttribute){
object.Call Property that implements attribute
}

Thanks
KK


Here's an example
HTH,
Andy
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Xox
{
class MyAttrib : Attribute { }
class XXX
{
[MyAttrib]
public int III
{
get { return 0; }
set { ; }
}
}
class Program
{
private static List<PropertyInfo> FindProps(Type tp, Type
attribType)
{
List<PropertyInfo> props = new List<PropertyInfo>();
PropertyInfo[] pis = tp.GetProperties();
for (int i = 0; i < pis.Length; ++i)
{
object[] attribs =
pis[i].GetCustomAttributes(typeof(MyAttrib), false);
for (int j = 0; j < attribs.Length; ++j)
{
if (attribType.IsAssignableFrom(attribs[j].GetType()))
{
props.Add(pis[i]);
break;
}
}
}
return props;
}
static void Main(string[] args)
{
List<PropertyInfo> props = FindProps(typeof(XXX),
typeof(MyAttrib));

XXX objectInstance = new XXX();

// so something with the props;
foreach (PropertyInfo pi in props)
{
// try to set
if (pi.CanWrite)
pi.SetValue(objectInstance, 5, null);
// try read
int i = -1;
if(pi.CanRead)
i = (int)pi.GetValue(objectInstance, null);
}
}
}
}
Dec 17 '05 #2

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

Similar topics

10
1603
by: Martin Miller | last post by:
I'm trying to create some read-only instance specific properties, but the following attempt didn't work: > class Foobar(object): > pass > > foobar = Foobar() > foobar.x =...
3
2687
by: George Sakkis | last post by:
I'm trying to write a decorator similar to property, with the difference that it applies to the defining class (and its subclasses) instead of its instances. This would provide, among others, a way...
3
5071
by: Martin Montgomery | last post by:
I have, for example, a property called myProperty. I would like, when using a property grid to display the property name as "My Property". Is this possible. Is there an attribute etc Thank ...
1
2457
by: The Last Gunslinger | last post by:
What are peoples views on using readonly properties to return the sum of a calculation? For instance you might have a invoice class that has itemprice and quantity properties. Would the total...
12
13343
by: Ray Cassick \(Home\) | last post by:
Ok, I have finally decided that there is ONE big thing about VB.NET (not sure if this same thing exists in C# yet) that really ticks me off. Either I am missing the point here or I have not found...
1
1022
by: KK | last post by:
Hi, I apply a custom Attribute to several properties in a class. There are about 15 of those. I want to know how I can programatically get ONLY those properties that I have applied my...
14
18254
by: Aaron Gray | last post by:
Hi, I want to access the properties of an IFrame but seem unable to get access to the IFrames document body. <html> <body> <iframe src="test.html" id="IFrame"></iframe> </body>
2
2269
by: Paul Melis | last post by:
Hello, The python library docs read in section 2.1 (http://docs.python.org/lib/built-in-funcs.html): " .... property( ]]]) Return a property attribute for new-style classes (classes that
5
5814
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Prior to C# 3.0, I would typically define a property of a custom user control with pattern A: === PATTERN A ========== private bool myBool = true; public bool MyBool { get { return myBool; }...
0
7203
marktang
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,...
1
6995
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...
0
7463
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
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5017
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...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
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 ...
0
389
bsmnconsultancy
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...

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.