反思-大会问题 [英] Reflection - Assembly Question

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

问题描述

我试图理解反射的概念,并在网上找到了一个教程.我以为我了解它,直到我开始更进一步地使用它为止.我创建了一个名为"reflectiontesting"的winform应用程序.我还创建了一个简单的类"clsCar",该类简单地覆盖了toString()方法并显示我是汽车".我在表单上有两个按钮.第一个按钮循环遍历当前应用程序域中的所有程序集,并简单地打印出它们的类型.这是代码:
foreach(AppDomain.CurrentDomain.GetAssemblies()中的程序集组件)
{
MessageBox.Show(assem.ToString());
}
第二个按钮创建Car类的实例,然后调用其toString方法.这是该代码.
System.Reflection.Assembly总成= System.Reflection.Assembly.Load("ReflectionTesting");
对象c = assem.CreateInstance("reflectionTesting.clsCar");
MessageBox.Show(c.ToString());
在运行并玩了这段代码之后,我以为我开始理解这个概念.然后,我进入该程序集的调试目录,并将可执行文件重命名为"reflectiontesting99.exe".完成此操作并单击button2后,我收到了FileNotFound异常.然后,我单击了button1,它显示程序集ReflectionTesting仍在加载.
我只是想知道为什么在生成可执行文件名称后更改它会导致此错误?
谢谢
Matt

I am trying to understand the concept of reflection and found a tutorial on the web. I thought I understood it until I started to play with it a little further. I created a winform app named ''reflectiontesting''. I also created a simple class called ''clsCar'' that simply overrides the toString() method and displays ''I am a car''. I have two buttons on the form. The first buttons loops through all of the assemblies in the current app domain and simply prints out their type. Here is the code:
foreach (Assembly assem in AppDomain.CurrentDomain.GetAssemblies())
{
MessageBox.Show(assem.ToString());
}
The second button creates an instance of the Car class and then calls its toString method. Here is the code for that.
System.Reflection.Assembly assem = System.Reflection.Assembly.Load("ReflectionTesting");
Object c = assem.CreateInstance("reflectionTesting.clsCar");
MessageBox.Show(c.ToString());
After running and playing with this code I thought I started to understand the concept. I then went into my debug directory for this assembly and renamed the executable to ''reflectiontesting99.exe''. Once I did this and clicked button2 I received a FileNotFound Exception. I then clicked button1 and it showed that the assembly ReflectionTesting was still loaded.
I am just wondering why changing the executable name after it has been produced causes this error?
Thanks
Matt

推荐答案

根据 MSDN [ ^ ],此方法可以抛出找不到文件异常.因此,当您提供名称时,显然是按文件名加载它,因此不会在范围内的所有目录中加载它可以看到的每个文件,并检查它们是否是.NET以及哪些程序集它们包含.
According to MSDN[^], this method can throw a file not found exception. So, when you provide a name, you''re obviously loading it by filename, it''s not going to load every file it can see, across all directories that are in scope, and check if they are .NET and what assemblies they contain.


重命名程序集时,您是否关闭了反射应用程序?好像您启动了反射应用程序,加载了程序集,将其重命名,然后尝试再次加载它.仍然会从button2的第一次单击中加载它,但是button2的第二次单击将导致错误,因为它再次尝试了相同的加载调用,而这次已命名的程序集不再存在.
Did you close down your reflection app when you renamed the assembly? It looks like you started your reflection app, loaded the assembly, renamed it, and tried to load it again. It would still be loaded from the first click of button2, but the second click of button2 would cause an error, because it''s trying the same load call again, and this time the named assembly is no longer present.


感谢您的回复.当我确实更改了.exe文件的名称时,该文件当时尚未运行.更改名称后,我便运行它并收到错误消息.
Thanks for your responses. When I did change the name of the .exe file it was not running at that point. After I changed the name I then ran it and got the error.


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

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