473,382 Members | 1,710 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,382 software developers and data experts.

Reflection and Attributes

Given an object/class, how do I (using reflection) get a list of properties that have a specific attribute assigned to them ? In this example, I want to obtain a list of all propertiesthat have the attribute 'FooAttribute' assigned to them. Many TIA[AttributeUsage( AttributeTargets.Property)]public class FooAttribute:Attribute{ bool _isFooBarred; FooAttribute() { _isFooBarred = false;}}public class Foo{ [FooAttribute()] public byte Property1 { get{}; set{}; } [FooAttribute()] public int Property2 { get{}; set{}; } public string Property3 { get{}; set{}; }}
Nov 17 '05 #1
1 2524
Here is one way to accomplish:

Type t = typeof (Foo);
MemberInfo[] mi = t.GetMembers(BindingFlags.GetProperty);
foreach (MemberInfo m in mi)
{
if (m.GetCustomAttributes(typeof(FooAttribute), false).Length > 0)
{
// FooAttribute defined for this property
}
}
"Jon Turner" <jj*@mail.com> wrote in message news:OF****************@TK2MSFTNGP12.phx.gbl...
Given an object/class, how do I (using reflection) get a list of properties that have a specific attribute assigned to them ? In this example, I want to obtain a list of all propertiesthat have the attribute 'FooAttribute' assigned to them. Many TIA [AttributeUsage( AttributeTargets.Property)]public class FooAttribute:Attribute{ bool _isFooBarred; FooAttribute() { _isFooBarred = false;}} public class Foo{ [FooAttribute()] public byte Property1 { get{}; set{}; } [FooAttribute()] public int Property2 { get{}; set{}; } public string Property3 { get{}; set{}; }}
Nov 17 '05 #2

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

Similar topics

0
by: Nigel Sampson | last post by:
ey all, I'm in the process of creating a method of tracking whenever an objects properties are changed. Since its not possible to alter existing types I decided to used a proxy generated using...
1
by: Hari | last post by:
Hello, I am quite new to C# and when reading over a couple of books discovered the presence of reflection and attributes. While, I do understand them a little, it would be great if I could know...
5
by: Wiktor Zychla | last post by:
Hello, I still have some problems with custom attributes and I ask someone for a helpful hand. In the appendix is the trivial ILAsm code with single custom attribute. The code compiles OK but...
1
by: Rich Noons | last post by:
Hi, I'm trying to recognise whether a function is deprecated or not when reflecting a method. eg. or
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
3
by: groovyjman21 | last post by:
Hi, I am using FXCop on my c# project and it is complaining about signing the assembly with a strong name key. All the documentation I can find says to do: using System.Reflection; .... ...
7
by: Paul Hadfield | last post by:
Hi, I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system...
4
by: =?Utf-8?B?QWJoaQ==?= | last post by:
I am using Reflection to invoke methods dynamically. I have got a special requirement where I need to pass a value to method by setting the custom method attribute. As I cannot change the...
8
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; i just started research reflection and i'm wondering if i have an empty class file can i use reflection to add member variables and attributes dynamically and then instantiate the class? What...
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.