Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 6-Jan-23 17:55pm
Comments
bsb25 16-May-11 3:04am    
can anyone provide examples

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-May-11 2:24am    
No! There are 5 (five) different access specifiers (not keywords, but access specifiers). Who can name them all is the good boy. Read the C# standard.
Besides, this does not answer OP question -- not at all.
--SA
walterhevedeich 16-May-11 2:34am    
Yeah your'e right, I missed internal protected. Thanks for the heads up. And your'e right again about this not answering OP's question. The question is not that clear so I decided to put an answer that can(hopefully) help him find out for himself. :)
Sergey Alexandrovich Kryukov 16-May-11 2:39am    
Thanks for understanding.

It looks like OP is already familiar with access specifiers.

I answered about access, please see. (I thought it deserves a complete formal answer, even though OP use may or may not be practical.)
--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
 
Share this answer
 
Comments
bsb25 16-May-11 3:01am    
i tried using the ffollowing code,but its throwing an error "object instance not ser to refer an onject"

Dose doseProperty = new Dose();
Type type = typeof(Dose );
Dose[] doseSuggestions=product.GetDoseSuggestions(pInfo, false);
FieldInfo fInfo = doseSuggestions[0].GetType().GetField("databaseDoseText");
string s = (String)fInfo.GetValue(doseProperty);
Sergey Alexandrovich Kryukov 16-May-11 4:23am    
I need to see exception stack to answer this question, but... I see.
OK, the warning I gave you in my first paragraph works, and also my instruction to use BindingFlags -- you failed to use it. Your problem is GetField. In this form, it tries to find only the public fields. As you say you're looking non-public, it returns null.

Another problem: you take that type of Dose, but later ignore it and take the type of doseSuggestions[0]. (By the way, how do you know doseSuggestions[0] exists? It is not the source of the problem as it would be "out of range" exception). What is the type of doseSuggestions[0]? It can be Dose but also a class derived from Dose, I don't know.

--SA
Chil Umez 16-May-11 3:08am    
Is this array (doseSuggestions) null???
or doseSuggestions[0] is null???
Sergey Alexandrovich Kryukov 16-May-11 4:15am    
I need to see the declaration to answer this question. If it was in an executable without source code, you could reflect its type and see. I don't have this information. Do you have the source code declaring this type. If this is and array, also its element type(s).
--SA
Albin Abel 16-May-11 6:40am    
I think using reflection only we can get meta data information of private members. Can't access or execute it. But if you are sure I will update my brain, no time to try it now :)
Create a Public Property you will be able to access it.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-May-11 2:37am    
The question was about non-public. Do you know how to access it?

If not, please see my answer. (I'm not telling it's practical. It's used in relatively rare cases, such as plug-ins...)
--SA
The simple answer is that the need to access a private member is a mistake, a design flaw.

One strategy used to allow different levels of access to Classes' internal methods/state is dealing with security issues at an OS level (groups, permissions, users).

Think abour how internet logins may keep user and admin rights to modify code; about how access to secure databases with varying levels of read/write permission.

Another strategy is to pass a security code of some type in the constructor of the Class: that's always a security risk.

If you didn't write the code, you can't modify it, etc., you are hacking the code ... a very undesirable task.
 
Share this answer
 
Comments
Dave Kreskowiak 7-Jan-23 10:56am    
This question is coming up on 12 years old. It's one of the targets being bombed by spammers.
BillWoodruff 7-Jan-23 12:51pm    
thanks, Dave, my brain is not doing DateTime that well, now. i thought Sergey might have come roaring back.

p.s. wondering why a spammer would choose an old QA question,
CHill60 12-Jan-23 11:54am    
"wondering why a spammer would choose an old QA question," - because they don't realise how the forum works and think they are hiding their spam away in the past. Not the brightest our spammers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900