C
C#2y ago
Pacrombie

❔ ✅ Reflection to get all Properties of a certain type

I have this in a class:
List<KeyCode> keyCodeProperties = typeof(SettingsData)
.GetProperties(System.Reflection.BindingFlags.Public)
.Select(property => property.GetValue(GameManager.Instance.Settings))
.ToList();
List<KeyCode> keyCodeProperties = typeof(SettingsData)
.GetProperties(System.Reflection.BindingFlags.Public)
.Select(property => property.GetValue(GameManager.Instance.Settings))
.ToList();
And this in the referenced Settings object:
public float MouseSens
{
get
{
return _mouseSens;
}
set
{
_mouseSens = value;
}
}
public KeyCode PrimaryFireKey
{
get
{
return _primaryFireKey;
}
set
{
_primaryFireKey = value;
}
}
public KeyCode AltFireKey
{
get
{
return _altFireKey;
}
set
{
_altFireKey = value;
}
}
...
public float MouseSens
{
get
{
return _mouseSens;
}
set
{
_mouseSens = value;
}
}
public KeyCode PrimaryFireKey
{
get
{
return _primaryFireKey;
}
set
{
_primaryFireKey = value;
}
}
public KeyCode AltFireKey
{
get
{
return _altFireKey;
}
set
{
_altFireKey = value;
}
}
...
The first block of code I sent does not compile because all the crap after the equals sign returns a list of objects, and I need a list of
KeyCode
KeyCode
s. I've hardly ever used Reflection and I'm just a little lost. I know my code as it stands (if I were to change my list to a
List<object>
List<object>
gets the values of all properties in my given class), but I just want the
KeyCode
KeyCode
properties in a List. Any help would be appreciated. I'm starting to think it would just be better to have them in a Dictionary in my Settings class since the names of the properties do matter (I could use Dictionary keys). Anyways, thank u in advance :)
27 Replies
Cattywampus
Cattywampus2y ago
is this Unity? if so, Keycode is Enum
Pacrombie
PacrombieOP2y ago
Yes
Cattywampus
Cattywampus2y ago
so you can just do Enum.GetValues(typeof(KeyCode)) then iterate it you don't need reflection at all
Pacrombie
PacrombieOP2y ago
I don't want it to be for every possible KeyCode, just the ones stored in my Settings properties
Cattywampus
Cattywampus2y ago
yes you can cache it somewhere then just access that?
Pacrombie
PacrombieOP2y ago
yeah im starting to think thats what ill have to do ill just have to refactor a bunch of stuff
ero
ero2y ago
what the just collect them in a list why use reflection
Pacrombie
PacrombieOP2y ago
I think I was looking to reflection as a lazy avoidance of rewriting what I did
ero
ero2y ago
public List<KeyCode> AllCodes => new List<KeyCode>
{
PrimaryFireKey,
AltFireKey,
};
public List<KeyCode> AllCodes => new List<KeyCode>
{
PrimaryFireKey,
AltFireKey,
};
Cattywampus
Cattywampus2y ago
reflection must be avoided if it's runtime stuff
Pacrombie
PacrombieOP2y ago
ah i dont really understand reflection then i think id have to use a dictionary because "PrimaryFireKey" would be something like KeyCode.Mouse0
ero
ero2y ago
nah, not really
Cattywampus
Cattywampus2y ago
you want to make a custom class for those actions
public class MyAction
{
...
public KeyCode ActionKey;
}
public class MyAction
{
...
public KeyCode ActionKey;
}
then make a List/dictionary
Pacrombie
PacrombieOP2y ago
so A property that is a list of properties?
ero
ero2y ago
what?
Pacrombie
PacrombieOP2y ago
AllCodes in what you sent is a list of the properties I already have?
ero
ero2y ago
and that's what you want?
Pacrombie
PacrombieOP2y ago
right, i just wanted to make sure im understanding what im looking at
ero
ero2y ago
specifically it adds the values of the properties
Pacrombie
PacrombieOP2y ago
right if i set those properties to something else, whatever took AllCodes would only have the values of what those properties' backing fields were at the time? so i should use AllCodes disposably?
ero
ero2y ago
that's how it's always gonna work
Pacrombie
PacrombieOP2y ago
i understand
ero
ero2y ago
that's kinda the issue when collecting things like these you should rather ask about the thing you're trying to achieve by collecting these values not about how to do the actual collection
Pacrombie
PacrombieOP2y ago
ah ive done something wrong at a base level if im at this point ive gone complete monkey i think i dont even think i need the list i think im really stupid
ero
ero2y ago
normal dev cycle
Pacrombie
PacrombieOP2y ago
real and true thank you though! !close
Accord
Accord2y ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server