views:

42

answers:

4

Im making a modular program, and it supports dynamic compilation of source files in the plugin directory.

What I would like to do, to speed up loading times, is save compiled assemblies to a separate folder. When my program loads, and comes across a source file to compile, i would like it to check if there is an already compiled assembly, and use it IF the source file has not been changed since then. If the source file is changed, then re-compile and override the saved assembly.

My question to you is, what would be an effective way, to track which source file belongs to which assembly, and an effective way to track whether a source file has been changed since last load or not.

A: 

The only way (that I can think of) to guarantee this 100% is to keep a copy of the original source file along with the compiled version. Then you can do a file comparison between the original and the new one to see if they've changed, and if so, perform your compilation.

jvenema
How exactly would you perform such a comparison?
Billy ONeal
Or keep a hash! See my answer for details.
Aren
What's up with the downvote? A simple binary comparison would work perfectly and be rocking fast.
jvenema
+1  A: 

Why would you want to do that exactly? Anyone who is going to be using C# to write a plugin for you is going to know how to use Visual Studio and build a DLL. You'd be much better off defining a DLL interface for plugins to use instead. Then you wouldn't have to worry about loading times of any sort.

If the "plugin" is supposed to be changed from inside your program itself, you should probably just compile when the plugin is changed in your program rather than attempting to see when things get changed.

Billy ONeal
I have a very dynamic plugin system, ill be doing lots of editing of plugins, reloading plugins, recompiling plugins etc, and its alot easier to just edit a source file and click a button than to open VS and recompile.
Tommy
@Tommy: Err... you're editing the plugins outside of VS? Why? And if it's just click a button, why not only recompile when the button is clicked (which is the second paragraph in my answer)?
Billy ONeal
My program is automated partly, it will be reloading on events, and i want to make this as fast as possible. hence only recompiling when needed.
Tommy
Oh, and it does support DLL's compiled in VS. Its just that I do need both.
Tommy
@Tommy: Yes, but for program startup time, there should never be a need to recompile on program start. And if there is a need to recompile on program start, it's not unreasonable to require users of your application to tell your program to recompile when needed. But seriously though, if opening up VS is too big a problem for you write a batch file that calls the C# compiler, or do it on top of MS build or something of that nature. Implementing this yourself is NIH madness.
Billy ONeal
The plugins will not only be compiled on startup, and you are assuming that this program communicates with the user. This is a console application in nature. Things need to be reloaded (plugins) while the program is running, so i need to know if things have been changed when reloading occurs. I realize that its not normal, but certain circumstances require certain measures.
Tommy
+3  A: 

Change Tracking: Keep MD5 / CRC Hashes of the source files on record & Last Modified date, correlate the two to determine if the files have changed.

As for source->assembly, I suggest convention over configuration.

Aren
Note: Billy ONeal is correct that your approach is probably flawed. You should use a normal plugin design for .NET, but I figured I'd answer your question bluntly.
Aren
The type of application im developing will not work properly with a regular .Net plugin design.. I didnt ask this question for criticism. I asked it because I need to do something a specific way for this situation and needed some insight.
Tommy
@Tommy: The reason you are being criticized is because what you are asking doesn't make much sense. When something doesn't make much sense, there will be people who answer you suggesting alternate solutions to the problem that can avoid the original problem you asked about. But if you [refuse to even think about the alternate solutions, instead berating those who took the time to write them for you](http://blogs.msdn.com/b/oldnewthing/archive/2010/07/28/10043237.aspx), you will be criticized.
Billy ONeal
Aren
A: 

My question to you is, what would be an effective way, to track which source file belongs to which assembly, and an effective way to track whether a source file has been changed since last load or not

One way could be to:

  1. Use FileSystemWatcher to track file changes
  2. Keep an list(or table) to maintain source-assembly file associations.
KMan