473,587 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reflection using BindingFlags

Hi guys,

I have a class which contains ~ 200 properties. Out of those 200 properties
I need to access 10 (which I know beforehand already) via reflection in
another class.
Currently I am doing the following with my context object which represents
the class containing the 200 properties:
Type t = context.GetType ();
foreach (PropertyInfo prop in t.GetProperties ()

This brings me all 200 properties. Is there a way to use the BindingFlags
parameter and configure in the context class which properties so be used and
which not? Something like that:

[UseForReflectio nAttribute]
public string frmOID
{
get { return "DoV1"; }
}

Thanks,

Martin
Jun 27 '08 #1
2 2197
(resend; NNTP issues?)

Not via BindingFlags - but you can via TypeDescriptor.

Marc

using System;
using System.Componen tModel;
class Foo
{
static void Main()
{
Foo foo = new Foo();
foo.DateOfBirth = DateTime.Today;
foo.Name = "Fred";
foo.ShoeSize = 10;
Attribute[] attribs = {new InterestingAttr ibute()};
foreach(Propert yDescriptor prop in
TypeDescriptor. GetProperties(f oo, attribs))
{
Console.WriteLi ne("{0}={1}", prop.Name,
prop.GetValue(f oo));
}
}

public string Name { get; set; }
[Interesting]
public DateTime DateOfBirth { get; set; }
[Interesting]
public int ShoeSize { get; set; }
}

[AttributeUsage( AttributeTarget s.Property,
AllowMultiple = false, Inherited = true)]
class InterestingAttr ibute : Attribute { }
Jun 27 '08 #2
If you wanted to use an attribute you'd have to enumerate through all of the
properties and test what attributes are on the property. If speed is an
issue, figure out which properties have the attribute applied to it for that
type and cache the results for easy retrieval later on. Once the class gets
checked over once, it won't have to worry about about finding them again. If
you need to have this handle across multiple objects, you may want to use an
in-memory collection similar to:

Dictionary<Type , Collection<Prop ertyInfo>cache;
Collection<Prop ertyInfopropert iesCollection = new
Collection<Prop ertyInfo>();

Type currentType = this.GetType();
foreach (PropertyInfo propInfo in currentType.Get Properties()) {
object[] attributes =
propInfo.GetCus tomAttributes(t ypeof(UseForRef lectionAttribut e), true));
if (attributes == null || attributes.Leng th == 0) continue;

propertiesColle ction.Add(propI nfo)
}

this.cache.Add( currentType, propertiesColle ction);

Now that the collection has been created for the current object, you'll want
to use it:

foreach (PropertyInfo propInfo in this.cache[this.GetType()]) {
// Do what you wanted to with your object here
}
Granted I don't know your situation, but something similar to what I've
demonstrated above should provide you with the flexibility you need and keep
it fast enough that you won't get a large degredation in speed while
enumerating through the properties on the object constantly.
"Martin Eckart" <moartl17ATyaho o.dewrote in message
news:e8******** ******@TK2MSFTN GP02.phx.gbl...
Hi guys,

I have a class which contains ~ 200 properties. Out of those 200
properties I need to access 10 (which I know beforehand already) via
reflection in another class.
Currently I am doing the following with my context object which represents
the class containing the 200 properties:
Type t = context.GetType ();
foreach (PropertyInfo prop in t.GetProperties ()

This brings me all 200 properties. Is there a way to use the BindingFlags
parameter and configure in the context class which properties so be used
and which not? Something like that:

[UseForReflectio nAttribute]
public string frmOID
{
get { return "DoV1"; }
}

Thanks,

Martin

Jun 27 '08 #3

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

Similar topics

9
3682
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can...
10
7350
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the...
2
864
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray that has the information, but we as developers have no way to access this information. I have come up with a solution for the problem, (as I am...
0
2150
by: James Hadwen | last post by:
Hi, I'm trying to do something with reflection that's a little wierd. The idea is that I call for a field to be updated on a base class. Using reflection it first updates the value, and then updates the change that has been made to a dictionary. The code below is just to encourage some ideas, if you look at most of the code you find that...
2
1900
by: Lev | last post by:
Hi, I have some code that does reflection on an assembly I load. When I try to get the attributes on one of the methods implemented in the assembly, the MC++ version does not return anything. See code below: Object __gc* memberAttributes = info->GetCustomAttributes(__typeof(EntryPointAttribute), false);
0
1541
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to execute button2's click event. This works great when i know the name of the method that i want to invoke. I do so by doing this: Dim...
15
11177
by: satankidneypie | last post by:
Hi, I'm going to start this off with some code as it'll make it easier to explain afterwards... using System; namespace ConsoleApplication1 { /// <summary>
5
4283
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create delegate: public class DelegateEx {
0
883
by: Gustavo Arriola | last post by:
Hello everybody! I'm trying to execute a method using Reflection. The code is as follows: public static void SoapHandler(Exception Error) { try { Type assemblyType;
6
2927
by: chandramohanp | last post by:
Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List<int> member. Following is the code. class TestClass { public int i = 0; public int IValue
0
8347
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...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
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...
0
5394
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...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
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
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.