Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Experiments / Attic / Winforms / AssemblyResolverDialog / Castle.Components.Winforms.AssemblyResolver / AssemblyResolverComponent.cs
blob9629faf53e9535538e37d92caca47f27e17388ee
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Components.Winforms.AssemblyResolver
17 using System;
18 using System.Reflection;
19 using System.Collections.Specialized;
20 using System.Windows.Forms;
22 using Castle.Core;
24 /// <summary>
25 /// This component installs an Assembly Resolver delegate that
26 /// asks the user for the location of the missing assembly. Once provided
27 /// it caches the location.
28 /// </summary>
29 [CastleComponent("assembly.resolver")]
30 public class AssemblyResolverComponent : IStartable
32 private String lastAssemblyPath;
33 private HybridDictionary name2Assembly = new HybridDictionary();
35 public AssemblyResolverComponent()
39 #region IStartable Members
41 public void Start()
43 AppDomain.CurrentDomain.AssemblyResolve +=
44 new ResolveEventHandler(CurrentDomain_AssemblyResolve);
47 public void Stop()
49 AppDomain.CurrentDomain.AssemblyResolve -=
50 new ResolveEventHandler(CurrentDomain_AssemblyResolve);
53 #endregion
55 private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
57 Assembly assembly = null;
59 String name = AssemblyUtils.Normalize(args.Name);
61 // Cache
62 if (name2Assembly.Contains(name))
64 return name2Assembly[name] as Assembly;
67 // Try to load from the last path
68 if (lastAssemblyPath != null)
70 assembly = AssemblyUtils.TryToLoadAssembly(lastAssemblyPath, name);
72 if (assembly != null)
74 UpdateCache(name, assembly);
75 return assembly;
79 // At last, we ask the user for the location
80 assembly = ShowForm(args, name);
82 if (assembly != null)
84 UpdateCache(name, assembly);
85 return assembly;
88 // All went wrong.
89 // Could not find it.
91 return null;
94 private Assembly ShowForm(ResolveEventArgs args, string name)
96 LocateAssemblyForm form = new LocateAssemblyForm(args.Name);
97 DialogResult result = DialogResult.Abort;
99 if (Form.ActiveForm != null)
101 result = form.ShowDialog(Form.ActiveForm);
103 else
105 result = form.ShowDialog();
108 if (result == DialogResult.OK)
110 lastAssemblyPath = form.AssemblyLocation;
112 return form.AssemblyLoaded;
115 return null;
118 private void UpdateCache(string name, Assembly loaded)
120 name2Assembly[name] = loaded;