访问非公共成员 [英] accessing non public members

查看:72
本文介绍了访问非公共成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何访问非公共成员?

我有一个名为mgoseSuggestiions的对象,该对象有一个非公共成员,称为频率"

我想访问相同的内容.

请Plz帮助我解决这个

How do we access non public members?

I have a object called doseSuggestiions which has a non public member called as "frequencies"

I want to access the same.

Plz help me in this

推荐答案

基本上,我们在C#中有4个访问修饰符.公共的,受保护的,内部的和私人的.以下是每个修饰符的定义:

1. 公共 [受保护的 [内部 [私有 [
Basically, we have 4 access modifiers in C#. public, protected, internal and private. Here are the definitions for each modifier:

1. The public [^] keyword is an access modifier for types and type members. Public access is the most permissive access level. There are no restrictions on accessing public members.

2. The protected[^] keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

3. The internal[^] keyword is an access modifier for types and type members. Internal members are accessible only within files in the same assembly. A common use of internal access is in component-based development because it enables a group of components to cooperate in a private manner without being exposed to the rest of the application code. For example, a framework for building graphical user interfaces could provide Control and Form classes that cooperate using members with internal access. Since these members are internal, they are not exposed to code that is using the framework.

4. The private[^] keyword is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared

So in short, depending on the non-public variable, there are different accessibility levels on your members.


创建一个公共属性,您将可以访问它.
Create a Public Property you will be able to access it.


只有一种方法可以访问非公共成员:反射.问题在于这种方法的支持性要差得多.例如,您可以按名称查找成员,但是如果您键入错误的字符串或稍后更改名称,则编译时检查不会警告您-您将找不到该成员.您可以找到所有成员,作为成员的多态数组,或者分别找到所有字段,所有属性,所有事件,所有方法,所有构造函数或实现的接口.您可以限制公共或非公共成员的搜索.

使用System.Type的以下方法:FindInterfaces, GetInterface, GetInterfaces, GetMember, GetMembers, GetMethod, GetMethods, GetProperty, GetProperties, GetEvent, GetEvents, GetConstructorImpl, GetConstructor.

请参阅
http://msdn.microsoft.com/en-us/library/system.type.aspx [^ ] .

要查找非公共方法,您需要使用带有System.Reflection.BindingFlags参数的"Get..."方法的版本.使用System.Reflection.BindingFlags.NonPublicSystem.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public

—SA
There is only one way to access non-public members: Reflection. The problem is that this method is much less supportable. For example, you can find a member by name, but if you mistype the string or name is changed later, compile-time check will not warn you — you won''t find the member. You can find all members collectively as a polymorphous array of members or separately all fields, all properties, all events, all methods, all constructors or implemented intefaces. You can limit the search by public or non-public members.

Use the following methods of System.Type: FindInterfaces, GetInterface, GetInterfaces, GetMember, GetMembers, GetMethod, GetMethods, GetProperty, GetProperties, GetEvent, GetEvents, GetConstructorImpl, GetConstructor.

See http://msdn.microsoft.com/en-us/library/system.type.aspx[^].

To find non-public methods, you need to use the versions of the "Get..." method using System.Reflection.BindingFlags parameter; use System.Reflection.BindingFlags.NonPublic or System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public

—SA


这篇关于访问非公共成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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