473,765 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to access private field?

zlf
Hi,
I am trying to access private field by code below. But it runs with
error.
Please tell me what's the problem or any method that may allow me to
access private field. Thank you
class Love
{
private string Name;
}

Love love = new Love();
Type type = love.GetType();
Object obj = type.InvokeMemb er(null,
BindingFlags.De claredOnly |
BindingFlags.Pu blic |
BindingFlags.No nPublic |
BindingFlags.In stance |
BindingFlags.Cr eateInstance, null, null, args);

string
str=(string)typ e.InvokeMember( "Name",BindingF lags.GetPropert y,null,obj,null );
Jun 1 '06 #1
3 5034
zlf
I have got the solution just now.

Love obj = new Love("test");
Type type = typeof(Love);

object obj2 = type.InvokeMemb er("Name",
BindingFlags.Ge tField | BindingFlags.No nPublic |
BindingFlags.In stance, null, obj, null);

Console.WriteLi ne(obj2);

test should be the output.
Jun 1 '06 #2
"zlf" <zl***@hotmail. com> a écrit dans le message de news:
%2************* ***@TK2MSFTNGP0 5.phx.gbl...

| Hi,
| I am trying to access private field by code below. But it runs with
| error.
| Please tell me what's the problem or any method that may allow me to
| access private field. Thank you

Look at Type.GetField(. ..) and FieldInfo.GetVa lue(...).

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 1 '06 #3
What is the exception you are getting?

It looks a little redundant to me. First, you are trying to call
InvokeMember, but you are not passing a member to invoke! Also, I would
think that this is only for executing properties and methods, not for
fetching a value from a field.

I would recommend getting the FieldInfo that corresponds to the field
you want the value of, and then calling the GetValue/SetValue methods to get
the value.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"zlf" <zl***@hotmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
Hi,
I am trying to access private field by code below. But it runs with
error.
Please tell me what's the problem or any method that may allow me to
access private field. Thank you
class Love
{
private string Name;
}

Love love = new Love();
Type type = love.GetType();
Object obj = type.InvokeMemb er(null,
BindingFlags.De claredOnly |
BindingFlags.Pu blic |
BindingFlags.No nPublic |
BindingFlags.In stance |
BindingFlags.Cr eateInstance, null, null, args);

string
str=(string)typ e.InvokeMember( "Name",BindingF lags.GetPropert y,null,obj,null );

Jun 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9199
by: Andrew Arace | last post by:
I scoured the groups for some hands on code to perform the menial task of exporting table data from an Access 2000 database to Oracle database (in this case, it was oracle 8i but i'm assuming this will work for 9i and even 10g ) No one had what I needed, so I wrote it myself. I Rule. This code isn't going for efficiency, and isn't trying to be dynamic. It doesn't create the table structure in Oracle, that's up to you. (I
8
3994
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI) using these scripts. EX: CREATE TABLE( Pk_myPrimaryKey INTEGER CONSTRAINT pk PRIMARY KEY DESCRIPTION 'This is the primary key of the table',
4
4639
by: Dennis C. Drumm | last post by:
Is there a way with C# to allow one class access to a method or field of another class, without making that method or field visible to all other classes, as would be the case when making the method or field public? Thanks, Dennis
13
3998
by: royaltiger | last post by:
I am trying to copy the inventory database in Building Access Applications by John L Viescas but when i try to run the database i get an error in the orders form when i click on the allocate button "Unexpected Error":3251 operation is not supported for this type of object.The demo cd has two databases, one is called inventory and the other just has the tables for the design called inventory data. When you run inventory the database works...
2
3227
by: Michael Bialowas | last post by:
Hello all, I have been searching the net endlessly and found this ng, so I thought I would give a try. Anyways, I have a few problems I am trying to implement a combo box which presently contains the following: Description, Price, and Treatment code. I wish to do the following: when the description field(combo box) is clicked I want the price, treatment code, insured field to become automated(update the corresponding data automatically)....
2
2200
by: Sam Shaw | last post by:
I have been looking after an MS Access database, using table links to access data in a back-end MDB database. We have recently micrated to a SQL Server 2000 back-end atabase, once again accessing it though table links. I am comfortable with the data side of things (whether through table links or an adodb.recordset). What I am missing, however, is all the useful things you could do to automatically maintain a back-end database from a...
4
12441
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
10
409
by: Les Desser | last post by:
In article <fcebdacd-2bd8-4d07-93a8-8b69d3452f3e@s50g2000hsb.googlegroups.com>, The Frog <Mr.Frog.to.you@googlemail.comMon, 14 Apr 2008 00:45:10 writes Thank you for that. It was very clear and I actually understand it!
2
19488
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.