1

I'm working with a class whose instances are used to manages various booleans.

public bool walkable = true;
public bool current = false;
public bool target = false;
public bool selectable = false;
public bool visited = false;
public Tile parent = null;
public int distance = 0;

There's also this Reset utility function, that will just set all of the booleans back to 0

public void Reset ()
{
    walkable = false;
    ...
}

Rather than writing out every attribute, I was hoping I could just have the function switch off any booleans that might belong to a given instance that this gets called on.

Poking around the internet I keep finding stuff on reflection, but as far as I have read this only works when one has a reference to the actual instance (not within the class definition), from the C# docs:

// Using GetType to obtain type information:  
int i = 42;  
System.Type type = i.GetType();  
System.Console.WriteLine(type);  

Is switching off the booleans from within the instance based on property possible? Is it a dumb thing to want to do? Perhaps I would be better off keeping track of the booleans in a Dictionary?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Daniel Thompson
  • 2,193
  • 4
  • 23
  • 37
  • 1
    You can use reflection to iterate over all class properties, check their type and set their value accordingly. This question has been asked nunerous times. – Adrian Sep 09 '18 at 11:25
  • The dictionary method would be my proposal, if you have a lot of properties. – Poul Bak Sep 09 '18 at 11:25
  • @Adriani6 every example I find though is targeting an instance -- can this be done from within an instance? If so, have any links? – Daniel Thompson Sep 09 '18 at 11:28
  • 1
    You can target an instance from within an instance by using the `this` keyword, e.g. `this.GetType().GetProperties().Where( p => p.PropertyType == typeof(bool))` – John Wu Sep 09 '18 at 11:39
  • Hmm its not.. propertyinfo is for properties. OP is asking for field.. – Gauravsa Sep 09 '18 at 13:17

2 Answers2

3

Rather than paying the cost of reflection, you could pay a much smaller cost by re-initializing an instance behind your facade.

public class AllThoseBooleans {
    // expose values
    public bool walkable => actual.walkable;
    public bool current => actual.current;
    public bool target => actual.target;

    // real values defined here.
    private class Actuals {
        private bool walkable {get; set;}
        private bool current {get; set;}
        private bool target {get; set;}
    }

    private Actuals actual {get; set;} = new Actuals();

    // reset all values to default by initialization
    public void ResetAll() {
        actual = new Actuals();
    }
}

Please note: I didn't run this or test the access modifiers; you might have to tweak that but the concept holds: your class of booleans can "have a" store that can be re-initialized much much cheaper than reflection.

clarkitect
  • 1,720
  • 14
  • 23
1

You can do this (Here Test is the class which has all the booleans):

Test test = new Test();

FieldInfo[] fields = typeof(Test).GetFields(); // Obtain all fields
foreach (var field in fields) // Loop through fields
{
     string name = field.Name; // Get string name
     object temp = field.GetValue(test); // Get value
     if (temp is bool) // if it is a bool.
         field.SetValue(test, false);

     Console.WriteLine("name: {0} value: {1}",field.Name, field.GetValue(test));
}

Input class:

public class Test
{
public bool walkable = true;
public bool current = true;
public bool target = true;
public bool selectable = true;
public bool visited = true;
public string parent = null;
public int distance = 0;
}

Output:

name: walkable value: False
name: current value: False
name: target value: False
name: selectable value: False
name: visited value: False
name: parent value: 
name: distance value: 0
Gauravsa
  • 6,330
  • 2
  • 21
  • 30