C# - 内部地产QUOT;可读QUOT;在快速监视但不使用反射? [英] C# - Internal Properties "readable" in quickwatch but not using reflection?

查看:140
本文介绍了C# - 内部地产QUOT;可读QUOT;在快速监视但不使用反射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到快速查看窗口可以访问所有的属性,不论访问限制(内部,保护,私人)库中的类,即使图书馆在一个完全不同的应用程序,LIB引用和命名空间。虽然我没有找到一个方法来访问这些使用反思。我特别想读(注 - 刚读)的程序集的内部属性。如果这是不可能通过怎样的内部的作品设计(同一个命名空间外面无法访问),为什么它是由VS.NET中的快速报价窗口读?

I see that the "Quick Watch" window has access to all properties, irrespective of access restrictions (internal, protected, private) of a class in a library, even when the library is referenced in a totally different app,lib and namespace. Whereas I am not finding a way to access these using "reflection". I am especially trying to "read" (note - just read) the internal property of an assembly. If this is not possible by design of how "internal" works (not accessible outside the same namespace), how come it is "read" by the "Quick watch" window in VS.NET?

下面是我使用的示例代码:

Here is the example code I used:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestLib
{
    public class TestInteralProp
    {
        internal string PropertyInternal
        {
            get { return "Internal Property!";  }
        }

        protected string PropertyProtected
        {
            get { return "Protected Property!"; }
        }

        string PropertyPrivate
        {
            get { return "Private Property!"; }
        }

        public string PropertyPublic
        {
            get { return "Public Property!";  }
        }

        protected internal string PropertyProtectedInternal
        {
            get { return "Protected Internal Property!"; }
        }
    }
}

当我创建一个对象对于TestInernalProp类,我可以看到在快速监视所有4个属性 -

When i create an object for TestInernalProp class, I can see all 4 properties in quickwatch -

当我使用反射来访问任何这些除了公共财产(PropertyPublic),我得到一个空引用异常。

And when I use reflection to access any of these except the public property (PropertyPublic), i get a null reference exception.

//this works
propTestObj.GetType().InvokeMember("PropertyPublic", System.Reflection.BindingFlags.GetProperty, null, propTestObj, null);
//this fails (obviously)
propTestObj.GetType().InvokeMember("PropertyInternal", System.Reflection.BindingFlags.GetProperty, null, propTestObj, null);

//this works
propTestObj.GetType().GetProperty("PropertyPublic").GetValue(propTestObj, null);
//this fails again
propTestObj.GetType().GetProperty("PropertyInternal").GetValue(propTestObj, null)

我不清楚快速查看怎样才能获得这些。

I am not clear about how "Quick Watch" can gain access to these.

推荐答案

使用这些标志

BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance



的getProperty 标志不此操作需要。你可能想添加静态以及

The GetProperty flag is not required for this operation. You might want to add Static as well.

请注意:你可以用 | ,因为它们的整数值是两个大国。请参见这个SO回答

Note: You can combine the flags with |, because their integer values are powers of two. See this SO answer.

注意(响应Lalman的和刀柄的有关思考的安全性问题的关注)

NOTE (In response to Lalman's and shanks's concern about security issues of Reflection)

有总是写不好的一种方式或危险代码。它是由你做不做。反思是不是做事的常规方式,而它是意味着专用工具编程,如O / R-映射器或分析工具。反思是非常强大的;但是,你应该明智地使用它。

There is always a way to write bad or dangerous code. It is up to you to do it or not. Reflection is not the regular way of doing things, rather is it meant for the programming of special tools, like o/r-mappers or analysis tools. Reflection is very powerful; however, you should use it wisely.

这篇关于C# - 内部地产QUOT;可读QUOT;在快速监视但不使用反射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆