Jon Skeet [C# MVP] wrote:
I have a class with many bool attributes.
Now I want to set them all at once to true or false, without forgetting
one or the other.
Any easy way to do that? (e.g. using reflection)
Reflection is basically the *only* way to do it. Now, which part of it
are you finding tricky? Are these *properties* you want to set, or
just fields? Have you used reflection before at all?
Jon
I never used reflection until now. The concerned elements are properties
and not just fields.
Currently I find both parts tricky. Finding the properties and setting them.
You can get your class using GetType method:
Type myType = Type.GetType("MyClass");
PropertyInfo pInfo = mytype.GetProperty("Property1") would give you
type infor abt Property1.
if(pInfo.PropertyType.Name == "bool")
pInfo.SetValue(myType, false,null);
This could help I hope. Please do some R&D, since I've not compiled
this code.- Hide quoted text -
- Show quoted text -