473,586 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reflection

QQ
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :

filedlg.Initial Directory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileNam e = "*.xml";
filedlg.Validat eNames = false;

if (filedlg.ShowDi alog() == DialogResult.Ca ncel)
return;

Type t = filedlg.GetType ();

PropertyInfo rowsPropertyInf o = t.GetProperty(" fileNames",
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e);
object[] o = (object[])rowsPropertyIn fo.GetValue(fil edlg,
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e,
null, null, null);

I got an error :

An unhandled exception of type 'System.NullRef erenceException ' occurred in
BGReport.exe

Additional information: Object reference not set to an instance of an
object.

Any idea ? Thanks.
Nov 16 '05 #1
9 6216
QQ <an*******@disc ussions.microso ft.com> wrote:
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :


<snip>

Please see my response to the same question you asked half an hour ago,
and read http://www.pobox.com/~skeet/csharp/faq/posting.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi,
Wrong text case on property name.
Use "FileNames" , and not "fileNames" .

cu
Adnan
"QQ" <an*******@disc ussions.microso ft.com> wrote in message news:OD******** ******@TK2MSFTN GP11.phx.gbl...
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :

filedlg.Initial Directory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileNam e = "*.xml";
filedlg.Validat eNames = false;

if (filedlg.ShowDi alog() == DialogResult.Ca ncel)
return;

Type t = filedlg.GetType ();

PropertyInfo rowsPropertyInf o = t.GetProperty(" fileNames",
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e);
object[] o = (object[])rowsPropertyIn fo.GetValue(fil edlg,
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e,
null, null, null);

I got an error :

An unhandled exception of type 'System.NullRef erenceException ' occurred in
BGReport.exe

Additional information: Object reference not set to an instance of an
object.

Any idea ? Thanks.
Nov 16 '05 #3
Hi,
Wrong text case on property name.
Use "FileNames" , and not "fileNames" .

cu
Adnan
"QQ" <an*******@disc ussions.microso ft.com> wrote in message news:OD******** ******@TK2MSFTN GP11.phx.gbl...
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :

filedlg.Initial Directory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileNam e = "*.xml";
filedlg.Validat eNames = false;

if (filedlg.ShowDi alog() == DialogResult.Ca ncel)
return;

Type t = filedlg.GetType ();

PropertyInfo rowsPropertyInf o = t.GetProperty(" fileNames",
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e);
object[] o = (object[])rowsPropertyIn fo.GetValue(fil edlg,
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e,
null, null, null);

I got an error :

An unhandled exception of type 'System.NullRef erenceException ' occurred in
BGReport.exe

Additional information: Object reference not set to an instance of an
object.

Any idea ? Thanks.
Nov 16 '05 #4
hi,

"fileNames" is actually a private field not
property. if you are referring to property, it should
be "FileNames" . if you are trying to get the value of
private field, this sample by theory should work:

using System;
using System.Collecti ons;
using System.Windows. Forms;
using System.Reflecti on;

public class MyClass : System.Windows. Forms.Form
{
private Button btn;

public static void Main()
{
Application.Run (new MyClass());
}

public MyClass()
{
btn = new Button();
btn.Click += new EventHandler(On Click);
btn.Text = "Click";
this.Controls.A dd(btn);
}

private static void OnClick(object sender,
EventArgs e)
{
MyClass2 c2 = new MyClass2();
FieldInfo field = c2.GetType().Ge tField
("field1", BindingFlags.In stance |
BindingFlags.No nPublic );
if (field!=null)
MessageBox.Show (field.GetValue
(c2).ToString() );
else
MessageBox.Show ("null");
}
}

public class MyClass2
{
private string field1 = "gani";
}

but unfortunately, i can't get the "fileNames" private
field of OpenFileDialog.

HTH,

gani
http://thedeveloperscorner.com.ph


-----Original Message-----
QQ <an*******@disc ussions.microso ft.com> wrote:
I am trying to use reflection to access the private property of OpenFileDialog fileNames property. I have the
following code :
<snip>

Please see my response to the same question you asked half an hour ago,and read http://www.pobox.com/~skeet/csharp/faq/posting.html
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 16 '05 #5
QQ
I tried it but failed.

<an*******@disc ussions.microso ft.com> wrote in message
news:18******** *************** ******@phx.gbl. ..
hi,

"fileNames" is actually a private field not
property. if you are referring to property, it should
be "FileNames" . if you are trying to get the value of
private field, this sample by theory should work:

using System;
using System.Collecti ons;
using System.Windows. Forms;
using System.Reflecti on;

public class MyClass : System.Windows. Forms.Form
{
private Button btn;

public static void Main()
{
Application.Run (new MyClass());
}

public MyClass()
{
btn = new Button();
btn.Click += new EventHandler(On Click);
btn.Text = "Click";
this.Controls.A dd(btn);
}

private static void OnClick(object sender,
EventArgs e)
{
MyClass2 c2 = new MyClass2();
FieldInfo field = c2.GetType().Ge tField
("field1", BindingFlags.In stance |
BindingFlags.No nPublic );
if (field!=null)
MessageBox.Show (field.GetValue
(c2).ToString() );
else
MessageBox.Show ("null");
}
}

public class MyClass2
{
private string field1 = "gani";
}

but unfortunately, i can't get the "fileNames" private
field of OpenFileDialog.

HTH,

gani
http://thedeveloperscorner.com.ph


-----Original Message-----
QQ <an*******@disc ussions.microso ft.com> wrote:
I am trying to use reflection to access the private property of OpenFileDialog fileNames property. I have the

following code :

<snip>

Please see my response to the same question you asked

half an hour ago,
and read

http://www.pobox.com/~skeet/csharp/faq/posting.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 16 '05 #6
QQ
I m accessing the private property of OpenFileDialog. So it is the correct
field (fileNames).

"adnan boz" <ab*********@ho tmail.com> wrote in message
news:#c******** ******@TK2MSFTN GP09.phx.gbl...
Hi,
Wrong text case on property name.
Use "FileNames" , and not "fileNames" .

cu
Adnan
"QQ" <an*******@disc ussions.microso ft.com> wrote in message
news:OD******** ******@TK2MSFTN GP11.phx.gbl...
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :

filedlg.Initial Directory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileNam e = "*.xml";
filedlg.Validat eNames = false;

if (filedlg.ShowDi alog() == DialogResult.Ca ncel)
return;

Type t = filedlg.GetType ();

PropertyInfo rowsPropertyInf o = t.GetProperty(" fileNames",
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e);
object[] o = (object[])rowsPropertyIn fo.GetValue(fil edlg,
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e,
null, null, null);

I got an error :

An unhandled exception of type 'System.NullRef erenceException ' occurred in
BGReport.exe

Additional information: Object reference not set to an instance of an
object.

Any idea ? Thanks.

Nov 16 '05 #7
QQ <an*******@disc ussions.microso ft.com> wrote:
I m accessing the private property of OpenFileDialog. So it is the correct
field (fileNames).


Hang on - is it a field or a property? The two are very different.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Hi,
Right, it is inaccessable.
So, you don't have the ReflectionPermi ssion. As described on
msdn(Type.GetPr operty Method (String, BindingFlags): If the requested type
is non-public and the caller does not have ReflectionPermi ssion to reflect
non-public objects outside the current assembly, this method returns a null
reference (Nothing in Visual Basic).)

But, why do you need the private fileNames?
So far I could examine, public FileNames returns the same thing,
except it uses FileIOPermissio n on every fileName clone.

Adnan
"QQ" <an*******@disc ussions.microso ft.com> wrote in message
news:uo******** ******@TK2MSFTN GP09.phx.gbl...
I m accessing the private property of OpenFileDialog. So it is the correct
field (fileNames).

"adnan boz" <ab*********@ho tmail.com> wrote in message
news:#c******** ******@TK2MSFTN GP09.phx.gbl...
Hi,
Wrong text case on property name.
Use "FileNames" , and not "fileNames" .

cu
Adnan
"QQ" <an*******@disc ussions.microso ft.com> wrote in message
news:OD******** ******@TK2MSFTN GP11.phx.gbl...
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :

filedlg.Initial Directory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileNam e = "*.xml";
filedlg.Validat eNames = false;

if (filedlg.ShowDi alog() == DialogResult.Ca ncel)
return;

Type t = filedlg.GetType ();

PropertyInfo rowsPropertyInf o = t.GetProperty(" fileNames",
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e);
object[] o = (object[])rowsPropertyIn fo.GetValue(fil edlg,
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e,
null, null, null);

I got an error :

An unhandled exception of type 'System.NullRef erenceException ' occurred in
BGReport.exe

Additional information: Object reference not set to an instance of an
object.

Any idea ? Thanks.
Nov 16 '05 #9
Reason is :

I have the following code :

OpenFileDialog filedlg = new OpenFileDialog( );

filedlg.Initial Directory = directory.Text;

filedlg.Filter = "lvl files (*.lvl)|*.lvl" ;
filedlg.FileNam e = "*.lvl";
filedlg.Validat eNames = false;

if (filedlg.ShowDi alog() == DialogResult.Ca ncel)
return;

string dirname = Path.GetDirecto ryName(filedlg. FileName);

I set the property ValidateNames to false. I didn't select any file when
the dialog box pop-ups. When I click the "Open" button, the dialog box was
closed. The problem is filedlg.FileNam e doesn't not contain anything. I
expect it to have something like this c:\Test\*.lvl (with Win32
OpenFileDialog, it will). The protected member of OpenFileDialog namely
fileNames and FileNameInterna l do have the value but as it is a protected
member, the value is inaccessible. ). With GetOpenFileName , i will be able
to get the result (C:\test\*.lvl) .
"adnan boz" <ab*********@ho tmail.com> wrote in message
news:uV******** ******@tk2msftn gp13.phx.gbl...
Hi,
Right, it is inaccessable.
So, you don't have the ReflectionPermi ssion. As described on
msdn(Type.GetPr operty Method (String, BindingFlags): If the requested type
is non-public and the caller does not have ReflectionPermi ssion to reflect
non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).)

But, why do you need the private fileNames?
So far I could examine, public FileNames returns the same thing,
except it uses FileIOPermissio n on every fileName clone.

Adnan
"QQ" <an*******@disc ussions.microso ft.com> wrote in message
news:uo******** ******@TK2MSFTN GP09.phx.gbl...
I m accessing the private property of OpenFileDialog. So it is the correct
field (fileNames).

"adnan boz" <ab*********@ho tmail.com> wrote in message
news:#c******** ******@TK2MSFTN GP09.phx.gbl...
Hi,
Wrong text case on property name.
Use "FileNames" , and not "fileNames" .

cu
Adnan
"QQ" <an*******@disc ussions.microso ft.com> wrote in message
news:OD******** ******@TK2MSFTN GP11.phx.gbl...
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :

filedlg.Initial Directory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileNam e = "*.xml";
filedlg.Validat eNames = false;

if (filedlg.ShowDi alog() == DialogResult.Ca ncel)
return;

Type t = filedlg.GetType ();

PropertyInfo rowsPropertyInf o = t.GetProperty(" fileNames",
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e);
object[] o = (object[])rowsPropertyIn fo.GetValue(fil edlg,
BindingFlags.In stance |
BindingFlags.St atic |
BindingFlags.Ge tProperty |
BindingFlags.Pu blic |
BindingFlags.No nPublic|
BindingFlags.Su ppressChangeTyp e,
null, null, null);

I got an error :

An unhandled exception of type 'System.NullRef erenceException ' occurred in BGReport.exe

Additional information: Object reference not set to an instance of an
object.

Any idea ? Thanks.

Nov 16 '05 #10

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

Similar topics

0
1369
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document what Reflection is (I can answer this question myself), what kind of Reflection Techniques there are (.NET Reflection, Python Reflection etc. I can...
10
7350
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the...
2
864
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray that has the information, but we as developers have no way to access this information. I have come up with a solution for the problem, (as I am...
2
1980
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This information would then be logged to a database along with some other information about the user. Thanks in advance. Mark
0
1541
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to execute button2's click event. This works great when i know the name of the method that i want to invoke. I do so by doing this: Dim...
3
2321
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided through an external file in which case we can use reflection to create objects of that type at run time. Case 1: The methods of those types can...
9
3842
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of an object can be constructed through reflection and method calls can be made via the reflection apis.
17
2297
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection to build a compiler? One that will construct a user defined class "on the fly" (literally, the user defines a class, instantiates it, and runs it...
0
1689
by: Gustavo Arriola | last post by:
Hola a todos! Estoy intentando ejecutar un método usando Reflection. El código es el siguiente: public static void SoapHandler(Exception Error) { try { Type assemblyType;
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7841
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...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
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...
0
6617
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...
0
3838
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...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
0
1184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.