❔ ✅ Reflection to get all Properties of a certain type
I have this in a class:
And this in the referenced Settings object:
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 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 gets the values of all properties in my given class), but I just want the 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
is this Unity?
if so, Keycode is Enum
Yes
so you can just do
Enum.GetValues(typeof(KeyCode))
then iterate it
you don't need reflection at allI don't want it to be for every possible KeyCode, just the ones stored in my Settings properties
yes you can cache it somewhere then just access that?
yeah im starting to think thats what ill have to do
ill just have to refactor a bunch of stuff
what the
just collect them in a list
why use reflection
I think I was looking to reflection as a lazy avoidance of rewriting what I did
reflection must be avoided if it's runtime stuff
ah i dont really understand reflection then
i think id have to use a dictionary
because "PrimaryFireKey" would be something like
KeyCode.Mouse0
nah, not really
you want to make a custom class for those actions
then make a List/dictionary
so
A property that is a list of properties?
what?
AllCodes in what you sent is a list of the properties I already have?
and that's what you want?
right, i just wanted to make sure im understanding what im looking at
specifically it adds the values of the properties
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?
that's how it's always gonna work
i understand
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
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
normal dev cycle
real and true
thank you though!
!close
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.