Set all bool attributes at once

  • Thread starter Thread starter Joe Kovac
  • Start date Start date
J

Joe Kovac

Hi!

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)

Thanks

Joe
 
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
 
Jon said:
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.
 
Something like below, then. If this is your own class, note that
storing lots of bools is less efficient than using a bit-mask
internally (e.g. a BitVector32, or a [Flags] enumeration, or just an
int).

object obj = null; // TODO
bool value = false;
foreach (PropertyDescriptor prop in
TypeDescriptor.GetProperties(obj)) {
if (!prop.IsReadOnly && prop.PropertyType ==
typeof(bool)) {
prop.SetValue(obj, value);
}
}

Marc
 
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.
 
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 -

Ok. Marc's method is the apt one for use I think. I was taking a long
route it seems.
 
It isn't really any more/less verbose - especially if you had used
GetProperties()... the only real difference is the distinction between
reflection and the component-model. I tend to try and work with the
latter where possible, largely because it is what binding uses, and it
works with virtual properties so can be more flexible. But your code
would have worked too.

Marc
 
Jon said:
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

Thanks to all, you brought me to the right context.
Though, I have one more problem:

Type t = typeof(StaticClass.SubClass);
// Static properties.
PropertyInfo[] pi = t.GetProperties(BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public);
foreach (PropertyInfo p in pi)
{
----> p.SetValue(StaticClass.SubClass, true, null);
}

My propeties are in a subclass of a static class. So I get following error:

Compiler Error Message: CS0119: 'StaticClass.SubClass' is a 'type',
which is not valid in the given context

I can "see" my properties. But I can`t set them.

Thanks for your help

Joe
 
My propeties are in a subclass of a static class. So I get following error:

Compiler Error Message: CS0119: 'StaticClass.SubClass' is a 'type',
which is not valid in the given context

I can "see" my properties. But I can`t set them.

Pass "null" as the first argument - you don't need to specify an
instance when the property is static.

Jon
 
Jon said:
Pass "null" as the first argument - you don't need to specify an
instance when the property is static.

Jon

THANKS so much! It works all perfectly now!

Joe
 
Hi,

"
My propeties are in a subclass of a static class. So I get following
error:

Compiler Error Message: CS0119: 'StaticClass.SubClass' is a 'type',
which is not valid in the given context

The first parameter of SetValue is the instance whose property you want to
change, as they are static there are no instance involved, just pass null.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top