|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Reflection; |
| 4 | +using ICSharpCode.Core; |
| 5 | + |
| 6 | +namespace ClarionAddins.AssemblyResolve |
| 7 | +{ |
| 8 | + class RegisterHandler : AbstractCommand |
| 9 | + { |
| 10 | + static String _nameSpace; |
| 11 | + public override void Run() |
| 12 | + { |
| 13 | + _nameSpace = GetType().ToString(); |
| 14 | + LoggingService.Debug(_nameSpace + " - Starting the Assembly resolver"); |
| 15 | + AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve); |
| 16 | + } |
| 17 | + |
| 18 | + static System.Reflection.Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) |
| 19 | + { |
| 20 | + |
| 21 | + //This handler is called only when the common language runtime tries to bind to the assembly and fails. |
| 22 | + LoggingService.Debug(_nameSpace + " - Assembly resolve requested for " + args.Name); |
| 23 | + Assembly MyAssembly = null; |
| 24 | + string strTempAssmbPath = String.Empty; |
| 25 | + |
| 26 | + // I was doing this but it seems that some assemblies (Clarion.core!!!) are in the BaseDirectory (and in later versions of clarion, also the backenbindings...) |
| 27 | + //Path.Combine(currentDomain.BaseDirectory, @"Addins\BackendBindings\ClarionBinding\") |
| 28 | + strTempAssmbPath = FindAssembly(args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll"); |
| 29 | + if (strTempAssmbPath != String.Empty) |
| 30 | + { |
| 31 | + LoggingService.Debug(_nameSpace + " - @@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Inside ResolveEventHandler @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 32 | + LoggingService.Debug(" args.Name=" + args.Name); |
| 33 | + LoggingService.Debug(" strTempAssmbPath=" + strTempAssmbPath); |
| 34 | + LoggingService.Debug(_nameSpace + " - @@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Inside ClarionEdge.SetTheme ResolveEventHandler @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 35 | + |
| 36 | + //Load the assembly from the specified path. |
| 37 | + MyAssembly = Assembly.LoadFrom(strTempAssmbPath); |
| 38 | + } |
| 39 | + //Return the loaded assembly. |
| 40 | + return MyAssembly; |
| 41 | + } |
| 42 | + |
| 43 | + private static string FindAssembly(string pAssemblyName) |
| 44 | + { |
| 45 | + AppDomain currentDomain = AppDomain.CurrentDomain; |
| 46 | + string[] files; |
| 47 | + string path; |
| 48 | + // First attempt to find it in the BaseDirectory |
| 49 | + path = currentDomain.BaseDirectory; |
| 50 | + LoggingService.Debug(_nameSpace + " - Check BaseDirectory (" + path + ")"); |
| 51 | + files = Directory.GetFiles(path, pAssemblyName, SearchOption.AllDirectories); |
| 52 | + if (files.Length > 0) |
| 53 | + { |
| 54 | + LoggingService.Debug(_nameSpace + " - Found in BaseDirectory, files[0]=" + files[0]); |
| 55 | + return files[0]; |
| 56 | + } |
| 57 | + |
| 58 | + // Or, try the addin directories |
| 59 | + // %appdata%\SoftVelocity\Clarion\8.0\Addins |
| 60 | + path = Path.Combine(PropertyService.ConfigDirectory, "AddIns"); |
| 61 | + LoggingService.Debug(_nameSpace + " - Check ConfigDirectory (" + path + ")"); |
| 62 | + files = Directory.GetFiles(path, pAssemblyName, SearchOption.AllDirectories); |
| 63 | + if (files.Length > 0) |
| 64 | + { |
| 65 | + LoggingService.Debug(_nameSpace + " - Found in ConfigDirectory, files[0]=" + files[0]); |
| 66 | + return files[0]; |
| 67 | + } |
| 68 | + |
| 69 | + // accessory |
| 70 | + path = FileUtility.Combine(new string[] { FileUtility.ApplicationRootPath, "Accessory", "AddIns" }); |
| 71 | + LoggingService.Debug(_nameSpace + " - Check ApplicationRootPath (" + path + ")"); |
| 72 | + files = Directory.GetFiles(path, pAssemblyName, SearchOption.AllDirectories); |
| 73 | + if (files.Length > 0) |
| 74 | + { |
| 75 | + LoggingService.Debug(_nameSpace + " - Found in ApplicationRootPath, files[0]=" + files[0]); |
| 76 | + return files[0]; |
| 77 | + } |
| 78 | + |
| 79 | + LoggingService.Debug(_nameSpace + " - Not Found!"); |
| 80 | + return String.Empty; |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | +} |
0 commit comments