473,657 Members | 2,546 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 MyCustomAttribu te){
object.Call Property that implements attribute
}

Thanks
KK
Dec 17 '05 #1
1 948
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 MyCustomAttribu te){
object.Call Property that implements attribute
}

Thanks
KK


Here's an example
HTH,
Andy
using System;
using System.Collecti ons.Generic;
using System.Reflecti on;

namespace Xox
{
class MyAttrib : Attribute { }
class XXX
{
[MyAttrib]
public int III
{
get { return 0; }
set { ; }
}
}
class Program
{
private static List<PropertyIn fo> FindProps(Type tp, Type
attribType)
{
List<PropertyIn fo> props = new List<PropertyIn fo>();
PropertyInfo[] pis = tp.GetPropertie s();
for (int i = 0; i < pis.Length; ++i)
{
object[] attribs =
pis[i].GetCustomAttri butes(typeof(My Attrib), false);
for (int j = 0; j < attribs.Length; ++j)
{
if (attribType.IsA ssignableFrom(a ttribs[j].GetType()))
{
props.Add(pis[i]);
break;
}
}
}
return props;
}
static void Main(string[] args)
{
List<PropertyIn fo> props = FindProps(typeo f(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(obj ectInstance, 5, null);
// try read
int i = -1;
if(pi.CanRead)
i = (int)pi.GetValu e(objectInstanc e, null);
}
}
}
}
Dec 17 '05 #2

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

Similar topics

10
1617
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 = property(fget=lambda: 42) > > print "foobar.x:", foobar.x
3
2688
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 to define the equivalent of class-level constants: class Foo(object): @classproperty def TheAnswer(cls): return "The Answer according to %s is 42" % cls.__name__
3
5074
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 Martin
1
2468
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 be implemented as readonly prop Total to return itemprice * quantity or method CalculateTotal which returns the same thing. A readonly prop is, IMHO, more intuitive as it is still an attribute of this class but what about when you have a longer...
12
13370
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 what I need to accomplish my need. I am creating a Windows forms user control and am inheriting from System.Windows.Forms.UserControl. There are several properties that are part of the Usercontrol class that I do not want to expose so I am...
1
1025
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 attribute. What I want is rather than calling those 15 attributes like;
14
18276
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
2277
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
5828
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; } set { myBool = value; } } ======================
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
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 we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.