反射 [英] Reflection

查看:60
本文介绍了反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用反射来访问

OpenFileDialog fileNames属性的私有属性。我有以下代码:


filedlg.InitialDirectory = directory.Text;


filedlg.Filter =" xml files(* .xml )| * .XML" ;

filedlg.FileName =" * .xml" ;;

filedlg.ValidateNames = false;


if(filedlg。 ShowDialog()== DialogResult.Cancel)

返回;


类型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);


我收到一个错误:




BGR中发生未处理的System.NullReferenceException类型异常eport.exe


附加信息:对象引用未设置为

对象的实例。


任意想法?谢谢。

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.

推荐答案

QQ< an ******* @ discussion.microsoft.com>写道:
QQ <an*******@discussions.microsoft.com> wrote:
我试图使用反射来访问
OpenFileDialog fileNames属性的私有属性。我有以下代码:
I am trying to use reflection to access the private property of
OpenFileDialog fileNames property. I have the following code :




< snip>


请参阅我对您问半个问题的回复小时前,

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

-

Jon Skeet - < ; sk *** @ pobox.com>
http://www.pobox .com / ~siget

如果回复小组,请不要给我发邮件



<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




属性名称上的文本案例错误。

使用FileNames,而不是fileNames。


cu

Adnan

" QQ" <一个******* @ discussions.microsoft.com>在消息新闻中写道:OD ************** @ TK2MSFTNGP11.phx.gbl ...

我试图用反射来访问<的私人财产br />
OpenFileDialog fileNames属性。我有以下代码:


filedlg.InitialDirectory = directory.Text;


filedlg.Filter =" xml files(* .xml )| * .XML" ;

filedlg.FileName =" * .xml" ;;

filedlg.ValidateNames = false;


if(filedlg。 ShowDialog()== DialogResult.Cancel)

返回;


类型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);


我收到一个错误:




BGR中发生未处理的System.NullReferenceException类型异常eport.exe


附加信息:对象引用未设置为

对象的实例。


任意想法?谢谢。
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.




属性名称上的文本大小错误。

使用FileNames,而不是"文件名" ;.


cu

Adnan

" QQ" <一个******* @ discussions.microsoft.com>在消息新闻中写道:OD ************** @ TK2MSFTNGP11.phx.gbl ...

我试图用反射来访问<的私人财产br />
OpenFileDialog fileNames属性。我有以下代码:


filedlg.InitialDirectory = directory.Text;


filedlg.Filter =" xml files(* .xml )| * .XML" ;

filedlg.FileName =" * .xml" ;;

filedlg.ValidateNames = false;


if(filedlg。 ShowDialog()== DialogResult.Cancel)

返回;


类型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);


我收到一个错误:




BGR中发生未处理的System.NullReferenceException类型异常eport.exe


附加信息:对象引用未设置为

对象的实例。


任意想法?谢谢。
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.


这篇关于反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆