C
C#2y ago
MrScautHD

❔ Is there a way to get a Private field?

Is there a way to get a Private field?
18 Replies
Thinker
Thinker2y ago
'Get' how?
MrScautHD
MrScautHDOP2y ago
like private Dictionary<string, ContentDefinition> _definitions;
nukleer bomb
nukleer bomb2y ago
This can be achieved by using reflection (however, fields are made private for a reason, so think about your code a little more before using it)
MrScautHD
MrScautHDOP2y ago
^^ how can i acces with reflection?
Thinker
Thinker2y ago
Firstly, why do you want this? It's almost always a bad idea
MrScautHD
MrScautHDOP2y ago
for a register system
Thinker
Thinker2y ago
Unless of course the private field is in the same class Do you own the class that the field is in?
MrScautHD
MrScautHDOP2y ago
no
Thinker
Thinker2y ago
right Wdym by "a register system"?
MrScautHD
MrScautHDOP2y ago
its a very complex thing but i try to acces to the list and add my stuff to it
Thinker
Thinker2y ago
you probably shouldn't add stuff to a private list
MrScautHD
MrScautHDOP2y ago
the owner of the framework just not thought about it and he is not at home so i need to find a temporary way
Thinker
Thinker2y ago
Well, you can use typeof(TheClass).GetField("nameOfField", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(instanceOfClass)
MrScautHD
MrScautHDOP2y ago
and how can i add now things to the list? i mean it does not work with the list
Thinker
Thinker2y ago
wdym? You get the value of the field as an object, then you have to cast that to the appropriate type
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Thinker
Thinker2y ago
they don't own the class
Accord
Accord2y ago
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