Getting type from new classes copied at runtime returns null
On a button push, my webapp copies several classes in to the project and th开发者_开发技巧en checks if the new files contain a class named my.namespace.myclass with Type.GetType("my.namespace.myclass", false, true); This returns null even though i can see the class with correct namespace, and if I write my.namespace.myclass inside my code(after refreshing files inside VS), the class is usable. If i click the same button again, the type is now found; i am guessing the class were not compiled before, but it is now.
Is there some way to force compile the files before trying to get the type? I tried restarting the asp.net application programmatically, but the type is still null.
Any help appreciated
I'm not sure if I understand your question correctly. What I understand is that you want to load a text file (.cs file are basicly text files) and use reflaction on the code inside it.
If I understand you correctly then you need to do some runtime compiling. It's not all that complicated, but still needs to be done. You can use the instructions here for this.
When you drop files into App_Code they are compiled into a separate assembly, and will cause your web app to recompile. The problem is, if you are copying them into App_Code, you won't see when you do Type.GetType(...) because your current version of App_Code (the compiled version currently running) doesn't contain them. I'd recommend trying to deliver them a separate way. App_Code is a special mechanism in ASP.NET, I wouldn't rely on this for practical applications. I'd rather have the classes compiled and dropped into another folder, where I can load them into the AppDomain and reflect where I need to.
精彩评论