public void Execute(ApplicationJob argument) { CompilerErrorCollection errors; Assembly assembly = Compile(out errors); if (errors.HasErrors) { throw new ApplicationException("Script cannot be compiled: " + errors[0].ErrorText); } // Now that we have a compiled script, lets run them foreach (Type type in assembly.GetExportedTypes()) { foreach (Type iface in type.GetInterfaces()) { if (iface != typeof(ICustomSetupScript)) { continue; } // yay, we found a script interface, lets create it and run it! // Get the constructor for the current type // you can also specify what creation parameter types you want to pass to it, // so you could possibly pass in data it might need, or a class that it can use to query the host application ConstructorInfo constructor = type.GetConstructor(System.Type.EmptyTypes); if (constructor != null && constructor.IsPublic) { // lets be friendly and only do things legitimitely by only using valid constructors // we specified that we wanted a constructor that doesn't take parameters, so don't pass parameters ICustomSetupScript scriptObject = constructor.Invoke(null) as ICustomSetupScript; if (scriptObject != null) { scriptObject.Execute(null, argument); } else { // hmmm, for some reason it didn't create the object // this shouldn't happen, as we have been doing checks all along, but we should // inform the user something bad has happened, and possibly request them to send // you the script so you can debug this problem // floele: Should not occur without an exception anyway. } } else { // and even more friendly and explain that there was no valid constructor // found and thats why this script object wasn't run // floele: Our scripts will automatically have a valid constructor. } } } }
public void Execute(ApplicationJob argument) { CompilerErrorCollection errors; Assembly assembly = Compile(out errors); if (errors.HasErrors) { throw new ApplicationException("Script cannot be compiled: " + errors[0].ErrorText); } // Now that we have a compiled script, lets run them foreach (Type type in assembly.GetExportedTypes()) { foreach (Type iface in type.GetInterfaces()) { if (iface != typeof(ICustomSetupScript)) { continue; } // yay, we found a script interface, lets create it and run it! // Get the constructor for the current type // you can also specify what creation parameter types you want to pass to it, // so you could possibly pass in data it might need, or a class that it can use to query the host application ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes); if (constructor != null && constructor.IsPublic) { // lets be friendly and only do things legitimitely by only using valid constructors // we specified that we wanted a constructor that doesn't take parameters, so don't pass parameters ICustomSetupScript scriptObject = constructor.Invoke(null) as ICustomSetupScript; if (scriptObject != null) { scriptObject.Execute(null, argument); } } } } }