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