To access a private static string array using reflection in C#, you can follow these steps:
typeof
operator.GetField
method to retrieve the FieldInfo object representing the private static string array.BindingFlags
parameter to include NonPublic
and Static
to access private and static members respectively.GetValue
method of the FieldInfo object to get the value of the private static string array.Here's an example code that demonstrates this:
main.cs508 chars24 lines
In the Main
method of the Program
class, we use typeof(MyClass)
to get the Type
object representing MyClass
. Then, we use GetField
method with the field name "myArray" and the appropriate BindingFlags
to retrieve the FieldInfo
object. Finally, we use GetValue
method on the FieldInfo
object, passing null
since the field is static, to get the value of the private static string array.
gistlibby LogSnag