Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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
Posted

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.
 
Share this answer
 
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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900