If i have a private class
Class A
{
public static string foo;
}
Can i use reflection to access that static field? Assuming of course i cannot change the code…
The problem i have is that the Class is defined in a different Namespace than i am in.
lets say I am in the Test namespace, and i have a reference to a DLL with the FOO namespace.
namespace FOO
{
Class A
{
public static string bar;
}
}
I want to access the bar field in the class A from namespace TEST.
Yes, you can. You’ll need to get the
Type– how you do that will depend on the exact nature of your app;Assembly.GetType(string)would be one option, for example. After that, you get theFieldInfowithType.GetFieldand then ask the field for its value, usingnullas the target as it’s a static field.