473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Reflection

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

filedlg.InitialDirectory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileName = "*.xml";
filedlg.ValidateNames = false;

if (filedlg.ShowDialog() == DialogResult.Cancel)
return;

Type t = filedlg.GetType();

PropertyInfo rowsPropertyInfo = t.GetProperty("fileNames",
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType);
object[] o = (object[])rowsPropertyInfo.GetValue(filedlg,
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType,
null, null, null);

I got an error :

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

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

Any idea ? Thanks.
Nov 16 '05 #1
9 6151
QQ <an*******@discussions.microsoft.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.com>
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*******@discussions.microsoft.com> wrote in message news:OD**************@TK2MSFTNGP11.phx.gbl...
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :

filedlg.InitialDirectory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileName = "*.xml";
filedlg.ValidateNames = false;

if (filedlg.ShowDialog() == DialogResult.Cancel)
return;

Type t = filedlg.GetType();

PropertyInfo rowsPropertyInfo = t.GetProperty("fileNames",
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType);
object[] o = (object[])rowsPropertyInfo.GetValue(filedlg,
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType,
null, null, null);

I got an error :

An unhandled exception of type 'System.NullReferenceException' 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*******@discussions.microsoft.com> wrote in message news:OD**************@TK2MSFTNGP11.phx.gbl...
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :

filedlg.InitialDirectory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileName = "*.xml";
filedlg.ValidateNames = false;

if (filedlg.ShowDialog() == DialogResult.Cancel)
return;

Type t = filedlg.GetType();

PropertyInfo rowsPropertyInfo = t.GetProperty("fileNames",
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType);
object[] o = (object[])rowsPropertyInfo.GetValue(filedlg,
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType,
null, null, null);

I got an error :

An unhandled exception of type 'System.NullReferenceException' 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.Collections;
using System.Windows.Forms;
using System.Reflection;

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(OnClick);
btn.Text = "Click";
this.Controls.Add(btn);
}

private static void OnClick(object sender,
EventArgs e)
{
MyClass2 c2 = new MyClass2();
FieldInfo field = c2.GetType().GetField
("field1", BindingFlags.Instance |
BindingFlags.NonPublic );
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*******@discussions.microsoft.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.com>
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*******@discussions.microsoft.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.Collections;
using System.Windows.Forms;
using System.Reflection;

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(OnClick);
btn.Text = "Click";
this.Controls.Add(btn);
}

private static void OnClick(object sender,
EventArgs e)
{
MyClass2 c2 = new MyClass2();
FieldInfo field = c2.GetType().GetField
("field1", BindingFlags.Instance |
BindingFlags.NonPublic );
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*******@discussions.microsoft.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.com>
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*********@hotmail.com> wrote in message
news:#c**************@TK2MSFTNGP09.phx.gbl...
Hi,
Wrong text case on property name.
Use "FileNames", and not "fileNames".

cu
Adnan
"QQ" <an*******@discussions.microsoft.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :

filedlg.InitialDirectory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileName = "*.xml";
filedlg.ValidateNames = false;

if (filedlg.ShowDialog() == DialogResult.Cancel)
return;

Type t = filedlg.GetType();

PropertyInfo rowsPropertyInfo = t.GetProperty("fileNames",
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType);
object[] o = (object[])rowsPropertyInfo.GetValue(filedlg,
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType,
null, null, null);

I got an error :

An unhandled exception of type 'System.NullReferenceException' 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*******@discussions.microsoft.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.com>
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 ReflectionPermission. As described on
msdn(Type.GetProperty Method (String, BindingFlags): If the requested type
is non-public and the caller does not have ReflectionPermission 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 FileIOPermission on every fileName clone.

Adnan
"QQ" <an*******@discussions.microsoft.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...
I m accessing the private property of OpenFileDialog. So it is the correct
field (fileNames).

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

cu
Adnan
"QQ" <an*******@discussions.microsoft.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :

filedlg.InitialDirectory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileName = "*.xml";
filedlg.ValidateNames = false;

if (filedlg.ShowDialog() == DialogResult.Cancel)
return;

Type t = filedlg.GetType();

PropertyInfo rowsPropertyInfo = t.GetProperty("fileNames",
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType);
object[] o = (object[])rowsPropertyInfo.GetValue(filedlg,
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType,
null, null, null);

I got an error :

An unhandled exception of type 'System.NullReferenceException' 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.InitialDirectory = directory.Text;

filedlg.Filter = "lvl files (*.lvl)|*.lvl" ;
filedlg.FileName = "*.lvl";
filedlg.ValidateNames = false;

if (filedlg.ShowDialog() == DialogResult.Cancel)
return;

string dirname = Path.GetDirectoryName(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.FileName 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 FileNameInternal 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*********@hotmail.com> wrote in message
news:uV**************@tk2msftngp13.phx.gbl...
Hi,
Right, it is inaccessable.
So, you don't have the ReflectionPermission. As described on
msdn(Type.GetProperty Method (String, BindingFlags): If the requested type
is non-public and the caller does not have ReflectionPermission 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 FileIOPermission on every fileName clone.

Adnan
"QQ" <an*******@discussions.microsoft.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...
I m accessing the private property of OpenFileDialog. So it is the correct
field (fileNames).

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

cu
Adnan
"QQ" <an*******@discussions.microsoft.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :

filedlg.InitialDirectory = directory.Text;

filedlg.Filter = "xml files (*.xml)|*.xml" ;
filedlg.FileName = "*.xml";
filedlg.ValidateNames = false;

if (filedlg.ShowDialog() == DialogResult.Cancel)
return;

Type t = filedlg.GetType();

PropertyInfo rowsPropertyInfo = t.GetProperty("fileNames",
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType);
object[] o = (object[])rowsPropertyInfo.GetValue(filedlg,
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic|
BindingFlags.SuppressChangeType,
null, null, null);

I got an error :

An unhandled exception of type 'System.NullReferenceException' 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
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...
10
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...
2
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...
2
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...
0
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...
3
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...
9
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...
17
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...
0
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.