Reflection

Q

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.
 
A

adnan boz

Hi,
Wrong text case on property name.
Use "FileNames", and not "fileNames".

cu
Adnan
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.
 
A

adnan boz

Hi,
Wrong text case on property name.
Use "FileNames", and not "fileNames".

cu
Adnan
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.
 
G

Guest

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
 
Q

QQ

I tried it but failed.

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
 
Q

QQ

I m accessing the private property of OpenFileDialog. So it is the correct
field (fileNames).

Hi,
Wrong text case on property name.
Use "FileNames", and not "fileNames".

cu
Adnan
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.
 
J

Jon Skeet [C# MVP]

QQ said:
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.
 
A

adnan boz

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


I m accessing the private property of OpenFileDialog. So it is the correct
field (fileNames).

Hi,
Wrong text case on property name.
Use "FileNames", and not "fileNames".

cu
Adnan
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.
 
T

Tank

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 said:
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


I m accessing the private property of OpenFileDialog. So it is the correct
field (fileNames).

Hi,
Wrong text case on property name.
Use "FileNames", and not "fileNames".

cu
Adnan
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top