Article From:
[C#]: how to access and invoke the private members and methods in the class

For original articles and source code for original code, such as reprint / copy, please mark the original name, author and web site in the page / code place, thank you!

Development tool: VS2017

Language: C#

DotNetVersion:.Net FrameWork 4 and above

System: Win10 X64

First, build a console APP and name it ConsoleApp, as shown in the following figure:

Two, create a new Person class, and add two members in the class, one is the name field, and the other is the Display method, which has a “prefix” input parameter prefix, which is added to the name field, as shown in the following code:

    class Person
        string name;
        void Display(string prefix)

Three. Introduce the System.Reflection. Here is the key to this essay.

using System.Reflection;

Four. How to set up and access the name field? Here we use reflection technology to access the reflection mechanism in the Type class.

1)The public part, the specific code is as follows:

 Person person = new Person();
 Type type = typeof(Person);
 BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
 FieldInfo fInfo = type.GetField("name", flags);

2)Set the name field and set the following code as follows:


3)To access the name field, the access method is shown in the following code:

var name = fInfo.GetValue(person);

4)nameThe result of the field is as follows:


BindingFlags.NonPublic:Search for members of non – public methods;

BindingFlags.Instance:Search member instances, including fields and methods, and so on.

Five. How to access the Display private method? Similarly, borrowing fourth parts of code (person and flags), the specific code is as follows:

MethodInfo mInfo = type.GetMethod("Display",flags);
mInfo.Invoke(person, new object[] { "Hello" });

The output results are as follows:


InvokeFor the call method, the latter parameters use the array method, and if there is one parameter, the object array of a parameter is entered, and if there are multiple parameters, the object array of multiple parameters is entered.

Six. The source code is as follows:

Seven, practical use, System.Windows.Forms under the name of a class named NotifyIcon, its set of Text can only be 63 characters, want to input more than 63 characters, the following setting method is not feasible, the code is as follows:

NotifyIcon ni = new NotifyIcon();
ni.Text = "64A character (actually has 64 characters, and does not reach a number here, for display only).";

Then we have to do something else to circumvent the limitation of NotifyIcon, which uses reflection technology (that is to say). The code is as follows:

NotifyIcon ni = new NotifyIcon();
string text = "A string of more than 63 characters, less than 127 characters";
if (text.Length >= 128) throw new ArgumentOutOfRangeException("Text limited to 127 characters");
Type t = typeof(NotifyIcon);
BindingFlags hidden = BindingFlags.NonPublic | BindingFlags.Instance;
t.GetField("text", hidden).SetValue(ni, text);
if ((bool)t.GetField("added", hidden).GetValue(ni)) t.GetMethod("UpdateIcon", hidden).Invoke(ni, new object[] { true });

The above limit number is more than 63, less than 127.

Leave a Reply

Your email address will not be published. Required fields are marked *