我可以使用 PrivateObject 类或 Reflection 来访问私有类中的字段吗?

发布于 2024-10-21 23:12:04 字数 376 浏览 2 评论 0原文

如果我有一个私有类,

Class A
{
    public static string foo;
}

我可以使用反射来访问该静态字段吗?当然,假设我无法更改代码...

我遇到的问题是该类是在与我所在的不同的命名空间中定义的。

假设我在 Test 命名空间中,并且我有一个带有 FOO 的 DLL 引用命名空间。

   namespace FOO
   {
     Class A
     {
         public static string bar;
     }
   }

我想从命名空间 TEST 访问类 A 中的 bar 字段。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜嗑 2024-10-28 23:12:04

是的,你可以。您需要获取Type - 如何获取取决于您的应用的确切性质; Assembly.GetType(string) 将是例如,一种选择。之后,您使用 Type.GetField 获取 FieldInfo,然后询问该字段的值,使用 null 作为目标,因为它是静态的场地。

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 the FieldInfo with Type.GetField and then ask the field for its value, using null as the target as it's a static field.

冷月断魂刀 2024-10-28 23:12:04

最终对我有用的是 Assembly 方法:

assembly = typeof(Class B).Assembly; //Class B is in the same namespace as A
Type type = assembly.GetType("FOO.A");
string val = (string) type.GetField("bar",
    BindingFlags.Public | BindingFlags.Static).GetValue(null);

What finally worked for me was the Assembly approach:

assembly = typeof(Class B).Assembly; //Class B is in the same namespace as A
Type type = assembly.GetType("FOO.A");
string val = (string) type.GetField("bar",
    BindingFlags.Public | BindingFlags.Static).GetValue(null);
汹涌人海 2024-10-28 23:12:04

这是故意冗长的,因此您将逐步了解正在发生的事情。它检查类型 A 中的所有字段并查找名为“foo”的字段。

编辑:它也适用于不同命名空间中的 A 。

namespace DifferentNamespace
{
    class A
    {
        public static string foo = "hello";
    }
}

class Program {
    static void Main(string[] args) {
        Type type = typeof(DifferentNamespace.A);
        FieldInfo[] fields = type.GetFields();
        foreach (var field in fields)
        {
            string name = field.Name;
            object temp = field.GetValue(null); // Get value
                                                // since the field is static 
                                                // the argument is ignored
                                                // so we can as well pass a null                
            if (name == "foo") // See if it is named "foo"
            {
                string value = temp as string;
                Console.Write("string {0} = {1}", name, value);
            }
            Console.ReadLine();
        }
    }
}

this is intentionally verbose so you'll get what is happening step by step. It checks all the fields in type A and looks for one named "foo".

EDIT: it works for A in a different namespace too.

namespace DifferentNamespace
{
    class A
    {
        public static string foo = "hello";
    }
}

class Program {
    static void Main(string[] args) {
        Type type = typeof(DifferentNamespace.A);
        FieldInfo[] fields = type.GetFields();
        foreach (var field in fields)
        {
            string name = field.Name;
            object temp = field.GetValue(null); // Get value
                                                // since the field is static 
                                                // the argument is ignored
                                                // so we can as well pass a null                
            if (name == "foo") // See if it is named "foo"
            {
                string value = temp as string;
                Console.Write("string {0} = {1}", name, value);
            }
            Console.ReadLine();
        }
    }
}
笑脸一如从前 2024-10-28 23:12:04

尝试

object value = typeof (Program).GetFields(BindingFlags.Static | BindingFlags.Public)
    .Where(x => x.Name == "foo").FirstOrDefault()
    .GetValue(null);

Try

object value = typeof (Program).GetFields(BindingFlags.Static | BindingFlags.Public)
    .Where(x => x.Name == "foo").FirstOrDefault()
    .GetValue(null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文