C
C#17mo ago
MrScautHD

❔ Is there a way to get a Private field?

Is there a way to get a Private field?
18 Replies
Thinker
Thinker17mo ago
'Get' how?
MrScautHD
MrScautHD17mo ago
like private Dictionary<string, ContentDefinition> _definitions;
nukleer bomb
nukleer bomb17mo 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
MrScautHD17mo ago
^^ how can i acces with reflection?
Thinker
Thinker17mo ago
Firstly, why do you want this? It's almost always a bad idea
MrScautHD
MrScautHD17mo ago
for a register system
Thinker
Thinker17mo ago
Unless of course the private field is in the same class Do you own the class that the field is in?
MrScautHD
MrScautHD17mo ago
no
Thinker
Thinker17mo ago
right Wdym by "a register system"?
MrScautHD
MrScautHD17mo ago
its a very complex thing but i try to acces to the list and add my stuff to it
Thinker
Thinker17mo ago
you probably shouldn't add stuff to a private list
MrScautHD
MrScautHD17mo 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
Thinker17mo ago
Well, you can use typeof(TheClass).GetField("nameOfField", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(instanceOfClass)
MrScautHD
MrScautHD17mo ago
and how can i add now things to the list? i mean it does not work with the list
Thinker
Thinker17mo 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 User17mo ago
Message Not Public
Sign In & Join Server To View
Thinker
Thinker17mo ago
they don't own the class
Accord
Accord17mo 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.