From ccc4e24de9ce989a0074b8fd212db9cbd7d636be Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 6 Jul 2010 15:44:04 +0000 Subject: [PATCH] Add workaround to ICSharpCode.Core that makes assemblies in the LoadFrom context also available in the Load context. This fixes some problems using WPF or .NET remoting in SharpDevelop AddIns. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6054 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- SharpDevelop.sln | 23 ++--- .../Analysis/CodeAnalysis/Src/FxCopWrapper.cs | 9 -- .../AddIn/Src/Views/ProfilerView.xaml.cs | 11 --- src/Main/Core/Project/ICSharpCode.Core.csproj | 1 + .../Core/Project/Src/AddInTree/AddIn/AddIn.cs | 1 + .../Src/AddInTree/AddIn/AssemblyLocator.cs | 50 +++++++++++ .../ICSharpCode.SharpDevelop.Dom.csproj | 1 - .../Src/ReflectionLayer/ReflectionLoader.cs | 89 ------------------- 8 files changed, 59 insertions(+), 126 deletions(-) create mode 100644 src/Main/Core/Project/Src/AddInTree/AddIn/AssemblyLocator.cs delete mode 100644 src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ReflectionLayer/ReflectionLoader.cs diff --git a/SharpDevelop.sln b/SharpDevelop.sln index a48844e1f1..3d059ead99 100644 --- a/SharpDevelop.sln +++ b/SharpDevelop.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 -# SharpDevelop 4.0.0.5950 +# SharpDevelop 4.0.0.6050 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Main", "Main", "{256F5C28-532C-44C0-8AB8-D8EC5E492E01}" ProjectSection(SolutionItems) = postProject EndProjectSection @@ -16,11 +16,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.SharpDevelop.Bu {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} = {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.Core.Tests", "src\Main\Core\Test\ICSharpCode.Core.Tests.csproj", "{AD6FAA08-D6F5-4DBA-AF85-F4DA9F40C3B5}" - ProjectSection(ProjectDependencies) = postProject - {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} = {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} - EndProjectSection -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.Core", "src\Main\Core\Project\ICSharpCode.Core.csproj", "{35CEF10F-2D4C-45F2-9DD1-161E0FEC583C}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.Core.Presentation", "src\Main\ICSharpCode.Core.Presentation\ICSharpCode.Core.Presentation.csproj", "{7E4A7172-7FF5-48D0-B719-7CD959DD1AC9}" @@ -438,27 +433,27 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Analysis", "Analysis", "{F3 ProjectSection(SolutionItems) = postProject EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTesting", "src\AddIns\Analysis\UnitTesting\UnitTesting.csproj", "{1F261725-6318-4434-A1B1-6C70CE4CD324}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeQualityAnalysis", "src\AddIns\Analysis\CodeQuality\CodeQualityAnalysis.csproj", "{76DD1CC0-0D86-44A1-9BD6-D91F79807BC3}" ProjectSection(ProjectDependencies) = postProject {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} = {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeAnalysis", "src\AddIns\Analysis\CodeAnalysis\CodeAnalysis.csproj", "{3EAA45A9-735C-4AC7-A799-947B93EA449D}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTesting", "src\AddIns\Analysis\UnitTesting\UnitTesting.csproj", "{1F261725-6318-4434-A1B1-6C70CE4CD324}" ProjectSection(ProjectDependencies) = postProject {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} = {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeCoverage", "src\AddIns\Analysis\CodeCoverage\Project\CodeCoverage.csproj", "{08CE9972-283B-44F4-82FA-966F7DFA6B7A}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeAnalysis", "src\AddIns\Analysis\CodeAnalysis\CodeAnalysis.csproj", "{3EAA45A9-735C-4AC7-A799-947B93EA449D}" ProjectSection(ProjectDependencies) = postProject {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} = {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceAnalysis", "src\AddIns\Analysis\SourceAnalysis\SourceAnalysis.csproj", "{CE498514-D12D-4B6E-AE0E-FEC29BD43748}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeCoverage", "src\AddIns\Analysis\CodeCoverage\Project\CodeCoverage.csproj", "{08CE9972-283B-44F4-82FA-966F7DFA6B7A}" ProjectSection(ProjectDependencies) = postProject {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} = {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeQualityAnalysis", "src\AddIns\Analysis\CodeQuality\CodeQualityAnalysis.csproj", "{76DD1CC0-0D86-44A1-9BD6-D91F79807BC3}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceAnalysis", "src\AddIns\Analysis\SourceAnalysis\SourceAnalysis.csproj", "{CE498514-D12D-4B6E-AE0E-FEC29BD43748}" ProjectSection(ProjectDependencies) = postProject {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} = {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} EndProjectSection @@ -691,10 +686,6 @@ Global {3DF4060F-5EE0-41CF-8096-F27355FD5511}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {3DF4060F-5EE0-41CF-8096-F27355FD5511}.Release|Any CPU.Build.0 = Release|Any CPU {3DF4060F-5EE0-41CF-8096-F27355FD5511}.Release|Any CPU.ActiveCfg = Release|Any CPU - {AD6FAA08-D6F5-4DBA-AF85-F4DA9F40C3B5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {AD6FAA08-D6F5-4DBA-AF85-F4DA9F40C3B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {AD6FAA08-D6F5-4DBA-AF85-F4DA9F40C3B5}.Release|Any CPU.Build.0 = Release|Any CPU - {AD6FAA08-D6F5-4DBA-AF85-F4DA9F40C3B5}.Release|Any CPU.ActiveCfg = Release|Any CPU {B22522AA-B5BF-4A58-AC6D-D4B45805521F}.Debug|Any CPU.Build.0 = Debug|Any CPU {B22522AA-B5BF-4A58-AC6D-D4B45805521F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B22522AA-B5BF-4A58-AC6D-D4B45805521F}.Release|Any CPU.Build.0 = Release|Any CPU @@ -848,7 +839,6 @@ Global {857CA1A3-FC88-4BE0-AB6A-D1EE772AB288} = {256F5C28-532C-44C0-8AB8-D8EC5E492E01} {7E4A7172-7FF5-48D0-B719-7CD959DD1AC9} = {256F5C28-532C-44C0-8AB8-D8EC5E492E01} {35CEF10F-2D4C-45F2-9DD1-161E0FEC583C} = {256F5C28-532C-44C0-8AB8-D8EC5E492E01} - {AD6FAA08-D6F5-4DBA-AF85-F4DA9F40C3B5} = {256F5C28-532C-44C0-8AB8-D8EC5E492E01} {C3CBC8E3-81D8-4C5B-9941-DCCD12D50B1F} = {256F5C28-532C-44C0-8AB8-D8EC5E492E01} {B5F54272-49F0-40DB-845A-8D837875D3BA} = {256F5C28-532C-44C0-8AB8-D8EC5E492E01} {DDE2A481-8271-4EAC-A330-8FA6A38D13D1} = {2A232EF1-EB95-41C6-B63A-C106E0C95D3C} @@ -933,5 +923,6 @@ Global {08CE9972-283B-44F4-82FA-966F7DFA6B7A} = {F355E45F-F54F-4B42-8916-9A633A392789} {3EAA45A9-735C-4AC7-A799-947B93EA449D} = {F355E45F-F54F-4B42-8916-9A633A392789} {1F261725-6318-4434-A1B1-6C70CE4CD324} = {F355E45F-F54F-4B42-8916-9A633A392789} + {76DD1CC0-0D86-44A1-9BD6-D91F79807BC3} = {F355E45F-F54F-4B42-8916-9A633A392789} EndGlobalSection EndGlobal diff --git a/src/AddIns/Analysis/CodeAnalysis/Src/FxCopWrapper.cs b/src/AddIns/Analysis/CodeAnalysis/Src/FxCopWrapper.cs index 8a7bb76e35..06e7107c5f 100644 --- a/src/AddIns/Analysis/CodeAnalysis/Src/FxCopWrapper.cs +++ b/src/AddIns/Analysis/CodeAnalysis/Src/FxCopWrapper.cs @@ -178,21 +178,12 @@ namespace ICSharpCode.CodeAnalysis setup.ApplicationBase = fxCopPath; AppDomain domain = AppDomain.CreateDomain("FxCop Rule Loading Domain", AppDomain.CurrentDomain.Evidence, setup); - ResolveEventHandler onResolve = delegate(object sender, ResolveEventArgs args) { - if (args.Name == typeof(FxCopWrapper).Assembly.FullName) { - return typeof(FxCopWrapper).Assembly; - } else { - return null; - } - }; FxCopRule[] ruleList; try { - AppDomain.CurrentDomain.AssemblyResolve += onResolve; FxCopWrapper wrapper = (FxCopWrapper)domain.CreateInstanceFromAndUnwrap(typeof(FxCopWrapper).Assembly.Location, typeof(FxCopWrapper).FullName); ruleList = wrapper.GetRuleListInstanceMethod(fxCopPath, ruleAssemblies); } finally { - AppDomain.CurrentDomain.AssemblyResolve -= onResolve; AppDomain.Unload(domain); } diff --git a/src/AddIns/Analysis/Profiler/Frontend/AddIn/Src/Views/ProfilerView.xaml.cs b/src/AddIns/Analysis/Profiler/Frontend/AddIn/Src/Views/ProfilerView.xaml.cs index 999eb970da..30a3a4a9ab 100644 --- a/src/AddIns/Analysis/Profiler/Frontend/AddIn/Src/Views/ProfilerView.xaml.cs +++ b/src/AddIns/Analysis/Profiler/Frontend/AddIn/Src/Views/ProfilerView.xaml.cs @@ -31,10 +31,7 @@ namespace ICSharpCode.Profiler.AddIn.Views public ProfilerView(ProfilingDataProvider provider) { - // workaround for bug in .NET 4.0 Beta 1 - AppDomain.CurrentDomain.AssemblyResolve += AppDomain_CurrentDomain_AssemblyResolve; InitializeComponent(); - AppDomain.CurrentDomain.AssemblyResolve -= AppDomain_CurrentDomain_AssemblyResolve; this.provider = provider; @@ -67,14 +64,6 @@ namespace ICSharpCode.Profiler.AddIn.Views InitializeOldTabs(); } - System.Reflection.Assembly AppDomain_CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) - { - if (args.Name == "ICSharpCode.Profiler.Controls, PublicKeyToken=null") - return typeof(QueryView).Assembly; - else - return null; - } - void TimeLineRangeChanged(object sender, RangeEventArgs e) { foreach (TabItem item in this.tabView.Items) { diff --git a/src/Main/Core/Project/ICSharpCode.Core.csproj b/src/Main/Core/Project/ICSharpCode.Core.csproj index cb0bb5d2da..a5e7a41962 100644 --- a/src/Main/Core/Project/ICSharpCode.Core.csproj +++ b/src/Main/Core/Project/ICSharpCode.Core.csproj @@ -58,6 +58,7 @@ + diff --git a/src/Main/Core/Project/Src/AddInTree/AddIn/AddIn.cs b/src/Main/Core/Project/Src/AddInTree/AddIn/AddIn.cs index 5a3e9beea8..72cdf75e0f 100644 --- a/src/Main/Core/Project/Src/AddInTree/AddIn/AddIn.cs +++ b/src/Main/Core/Project/Src/AddInTree/AddIn/AddIn.cs @@ -89,6 +89,7 @@ namespace ICSharpCode.Core // However, we need to make sure we don't return before the dependencies are ready, // so "bool dependenciesLoaded" must be volatile and set only at the very end of this method. if (!dependenciesLoaded) { + AssemblyLocator.Init(); foreach (AddInReference r in manifest.Dependencies) { if (r.RequirePreload) { bool found = false; diff --git a/src/Main/Core/Project/Src/AddInTree/AddIn/AssemblyLocator.cs b/src/Main/Core/Project/Src/AddInTree/AddIn/AssemblyLocator.cs new file mode 100644 index 0000000000..c41af5fbc7 --- /dev/null +++ b/src/Main/Core/Project/Src/AddInTree/AddIn/AssemblyLocator.cs @@ -0,0 +1,50 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace ICSharpCode.Core +{ + // Based on http://ayende.com/Blog/archive/2006/05/22/SolvingTheAssemblyLoadContextProblem.aspx + // This class ensures that assemblies loaded into the LoadFrom context are also available + // in the Load context. + static class AssemblyLocator + { + static Dictionary assemblies = new Dictionary(); + static bool initialized; + + public static void Init() + { + lock (assemblies) { + if (initialized) + return; + initialized = true; + AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad; + AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; + } + } + + static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + lock (assemblies) { + Assembly assembly = null; + assemblies.TryGetValue(args.Name, out assembly); + return assembly; + } + } + + static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args) + { + Assembly assembly = args.LoadedAssembly; + lock (assemblies) { + assemblies[assembly.FullName] = assembly; + } + } + } +} diff --git a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/ICSharpCode.SharpDevelop.Dom.csproj b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/ICSharpCode.SharpDevelop.Dom.csproj index 4874dfe242..ecf8ebf0a3 100644 --- a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/ICSharpCode.SharpDevelop.Dom.csproj +++ b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/ICSharpCode.SharpDevelop.Dom.csproj @@ -132,7 +132,6 @@ - diff --git a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ReflectionLayer/ReflectionLoader.cs b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ReflectionLayer/ReflectionLoader.cs deleted file mode 100644 index 6a28764a78..0000000000 --- a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ReflectionLayer/ReflectionLoader.cs +++ /dev/null @@ -1,89 +0,0 @@ -// -// -// -// -// $Revision$ -// - -using System; -using System.IO; -using System.Reflection; - -namespace ICSharpCode.SharpDevelop.Dom -{ - public sealed class ReflectionLoader : MarshalByRefObject - { - public override string ToString() - { - return "ReflectionLoader in " + AppDomain.CurrentDomain.FriendlyName; - } - - public string LoadAndCreateDatabase(string fileName, string cacheDirectory) - { - try { - ReflectionProjectContent content = LoadProjectContent(fileName, new ProjectContentRegistry()); - if (content == null) - return null; - return new DomPersistence(cacheDirectory, null).SaveProjectContent(content); - } catch (Exception ex) { - if (ex is FileLoadException) { - LoggingService.Info(ex); - } else { - LoggingService.Error(ex); - } - throw; - } - } - - ReflectionProjectContent LoadProjectContent(string fileName, ProjectContentRegistry registry) - { - fileName = Path.GetFullPath(fileName); - LoggingService.Debug("Trying to load " + fileName); - Assembly assembly; - AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += AssemblyResolve; - lookupDirectory = Path.GetDirectoryName(fileName); - try { - if (File.Exists(fileName)) { - assembly = Assembly.ReflectionOnlyLoadFrom(fileName); - return new ReflectionProjectContent(assembly, fileName, registry); - } else - throw new FileLoadException("Assembly not found."); - } catch (BadImageFormatException ex) { - LoggingService.Warn("BadImageFormat: " + fileName); - throw new FileLoadException(ex.Message, ex); - } finally { - AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= AssemblyResolve; - lookupDirectory = null; - } - } - - string lookupDirectory; - - Assembly AssemblyResolve(object sender, ResolveEventArgs e) - { - AssemblyName name = new AssemblyName(e.Name); - LoggingService.Debug("ProjectContentRegistry.AssemblyResolve " + e.Name); - string path = Path.Combine(lookupDirectory, name.Name); - if (File.Exists(path + ".dll")) { - return Assembly.ReflectionOnlyLoadFrom(path + ".dll"); - } - if (File.Exists(path + ".exe")) { - return Assembly.ReflectionOnlyLoadFrom(path + ".exe"); - } - if (File.Exists(path)) { - return Assembly.ReflectionOnlyLoadFrom(path); - } - try { - LoggingService.Debug("AssemblyResolve trying ReflectionOnlyLoad"); - return Assembly.ReflectionOnlyLoad(e.Name); - } catch (FileNotFoundException) { - LoggingService.Warn("AssemblyResolve: ReflectionOnlyLoad failed for " + e.Name); - // We can't get the assembly we want. - // But propably we can get a similar version of it. - DomAssemblyName fixedName = GacInterop.FindBestMatchingAssemblyName(e.Name); - LoggingService.Info("AssemblyResolve: FixedName: " + fixedName); - return Assembly.ReflectionOnlyLoad(fixedName.FullName); - } - } - } -}