473,771 Members | 2,297 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 6242
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
1382
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 answer this one myself too). The main question I need to answer is related to the implementation of...
10
7370
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 parent COM object from the control (Parent property is null :( ). I have stopped the control in 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 sure many others have) using reflection. Here is some sample code that will print out the auto...
2
2014
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
1548
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 AssemblyPointer As Reflection.Assembly
3
2330
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 be invoked through reflection. Case 2: The caller can publish certain events. The types (objects)...
9
3849
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
2310
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 from the console mode, all the while prompted by the program)? I guess so, but my final question...
0
1702
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
9619
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
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
10102
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.