471,337 Members | 1,102 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 972
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Martin Miller | last post: by
3 posts views Thread by George Sakkis | last post: by
3 posts views Thread by Martin Montgomery | last post: by
1 post views Thread by The Last Gunslinger | last post: by
12 posts views Thread by Ray Cassick \(Home\) | last post: by
14 posts views Thread by Aaron Gray | last post: by
2 posts views Thread by Paul Melis | last post: by
17 posts views Thread by David C. Ullrich | last post: by
5 posts views Thread by =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post: by

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.