/// <summary> /// clone instance /// </summary> /// <returns>instance</returns> object System.ICloneable.Clone() { DotNetAssemblyInfoList list = new DotNetAssemblyInfoList(); list.InnerList.AddRange(this); return(list); }
/// <summary> /// initialize instance /// </summary> /// <param name="name">Assembly name , can be CodeBase or short name</param> public DotNetAssemblyInfo(string name) { if (name == null) { throw new ArgumentNullException("name"); } name = name.Trim(); if (name.Length == 0) { throw new ArgumentNullException("name"); } if (name.IndexOf(":") > 0) { Uri uri = new Uri(name); if (uri.Scheme == Uri.UriSchemeFile) { strFileName = uri.LocalPath; } else { strFileName = uri.AbsoluteUri; } strName = System.IO.Path.GetFileNameWithoutExtension(strFileName); } else { if (name.ToLower().EndsWith(".dll") || name.ToLower().EndsWith(".exe")) { strName = System.IO.Path.GetFileNameWithoutExtension(name); strFileName = name; } else { strName = name; strFileName = name + ".dll"; } } if (DotNetAssemblyInfoList.IsInRuntimePath(strFileName)) { intSourceStyle = AssemblySourceStyle.Standard; } else { intSourceStyle = AssemblySourceStyle.ThirdPart; } }
/// <summary> /// Compile script /// </summary> /// <returns>Compiler ok or not</returns> public virtual bool Compile() { strRuntimeScriptText = null; strCompilerOutput = null; myCompilerErrors = null; if (bolClosedFlag) { return(false); } if (strScriptText == null || strScriptText.Trim().Length == 0) { return(false); } intScriptVersion++; bool bolResult = false; this.ScriptMethods.Clear(); myAssembly = null; bolScriptModified = false; // Generate integrated VB.NET source code string ModuleName = "mdlXVBAScriptEngine"; string globalModuleName = "mdlXVBAScriptGlobalValues"; string nsName = "NameSpaceXVBAScriptEngien"; System.Text.StringBuilder mySource = new System.Text.StringBuilder(); mySource.Append("Option Strict Off"); foreach (string import in this.Options.ImportNamespaces) { mySource.Append("\r\nImports " + import); } mySource.Append("\r\nNamespace " + nsName); //System.Collections.Hashtable myGObjects = new Hashtable(); // Generate source code for global objects if (myGlobalObjects != null && myGlobalObjects.Count > 0) { myVBCompilerImports.Add(nsName); //mySource.Append("\r\n<Microsoft.VisualBasic.CompilerServices.StandardModule>"); mySource.Append("\r\nModule " + globalModuleName); mySource.Append("\r\n"); mySource.Append("\r\n Public myGlobalValues As Object"); foreach (XVBAScriptGlobalObject item in myGlobalObjects) { if (System.Xml.XmlReader.IsName(item.Name) == false) { if (this.ThrowException) { throw new ArgumentException("Script global object name:" + item.Name); } else { if (this.OutputDebug) { System.Diagnostics.Debug.WriteLine("Script global object name:" + item.Name); } continue; } } if (item.ValueType.Equals(typeof(object)) || item.ValueType.IsPublic == false) { mySource.Append("\r\n Public ReadOnly Property " + item.Name + "() As Object "); mySource.Append("\r\n Get"); mySource.Append("\r\n Return myGlobalValues(\"" + item.Name + "\") "); mySource.Append("\r\n End Get"); mySource.Append("\r\n End Property"); } else { string typeName = item.ValueType.FullName; typeName = typeName.Replace("+", "."); mySource.Append("\r\n Public ReadOnly Property " + item.Name + "() As " + typeName); mySource.Append("\r\n Get"); mySource.Append("\r\n Return CType( myGlobalValues(\"" + item.Name + "\") , " + typeName + ")"); mySource.Append("\r\n End Get"); mySource.Append("\r\n End Property"); } } mySource.Append("\r\nEnd Module"); } mySource.Append("\r\nModule " + ModuleName); mySource.Append("\r\n"); mySource.Append(this.strScriptText); mySource.Append("\r\nEnd Module"); mySource.Append("\r\nEnd Namespace"); strRuntimeScriptText = mySource.ToString(); myAssembly = null; if (myAssemblyBuffer != null) { // Check assembly buffer myAssembly = myAssemblyBuffer.GetAssembly(strRuntimeScriptText); } if (myAssembly == null) { // Use dynamic compile DynamicCompiler compiler = new DynamicCompiler(); compiler.Language = CompilerLanguage.VB; compiler.AppDomain = myAppDomain; compiler.PreserveAssemblyFile = false; compiler.OutputDebug = this.OutputDebug; compiler.ThrowException = this.ThrowException; compiler.SourceCode = strRuntimeScriptText; compiler.AutoLoadResultAssembly = true; // add referenced assemblies DotNetAssemblyInfoList refs = this.Options.ReferenceAssemblies.Clone(); refs.AddByType(this.GetType()); if (this.GlobalObjects.Count > 0) { foreach (XVBAScriptGlobalObject item in this.GlobalObjects) { refs.AddByType(item.ValueType); } } foreach (string asm in this.Options.InnerReferenceAssemblies) { refs.AddByName(asm); } foreach (DotNetAssemblyInfo asm in refs) { if (asm.SourceStyle == AssemblySourceStyle.Standard) { compiler.ReferenceAssemblies.Add(System.IO.Path.GetFileName(asm.FileName)); } else { compiler.ReferenceAssemblies.Add(asm.FileName); } } foreach (string ns in this.VBCompilerImports) { compiler.CompilerImports.Add(ns); } if (compiler.Compile()) { myAssembly = compiler.ResultAssembly; if (myAssemblyBuffer != null) { myAssemblyBuffer.AddAssembly( strRuntimeScriptText, myAssembly, compiler.ResultAssemblyBinary); } bolResult = true; } else { myCompilerErrors = compiler.CompilerErrors; foreach (CompilerError error in myCompilerErrors) { if (error.IsWarning == false) { // report error if (this.Errors != null) { ScriptError err = new ScriptError(this, ScriptErrorStyle.Compile, null, null); err.Message = error.ErrorText; err.ScriptText = error.ErrorText; this.Errors.Add(err); } bolResult = false; break; } } } strRuntimeScriptTextWithCompilerErrorMessage = compiler.SourceCodeWithCompilerErrorMessage; } if (this.myAssembly != null) { // set global object instance Type ModuleType = myAssembly.GetType(nsName + "." + globalModuleName); if (ModuleName != null) { System.Reflection.FieldInfo gf = ModuleType.GetField("myGlobalValues"); gf.SetValue(null, this.GlobalObjects); } // analyze script method ModuleType = myAssembly.GetType(nsName + "." + ModuleName); if (ModuleType != null) { System.Reflection.MethodInfo[] ms = ModuleType.GetMethods( System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); foreach (System.Reflection.MethodInfo m in ms) { // generate script method information ScriptMethodInfo info = new ScriptMethodInfo(); info.Assembly = myAssembly; info.MethodName = m.Name; info.MethodObject = m; info.ModuleName = ModuleType.Name; info.ReturnType = m.ReturnType; info.ScriptText = (string)myMethodSciptText[info.MethodName]; this.ScriptMethods.Add(info); if (this.bolOutputDebug) { System.Diagnostics.Debug.WriteLine( string.Format(ScriptStrings.AnalyseVBMethod_Name, m.Name)); } } //foreach bolResult = true; } //if } return(bolResult); }
/// <summary> /// refrsh global assembly list /// </summary> public static void RefreshGlobalList() { myGlobalList = null; }