1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Reflection
;
19 using System
.Collections
.Specialized
;
20 using System
.Windows
.Forms
;
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.
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
43 AppDomain
.CurrentDomain
.AssemblyResolve
+=
44 new ResolveEventHandler(CurrentDomain_AssemblyResolve
);
49 AppDomain
.CurrentDomain
.AssemblyResolve
-=
50 new ResolveEventHandler(CurrentDomain_AssemblyResolve
);
55 private Assembly
CurrentDomain_AssemblyResolve(object sender
, ResolveEventArgs args
)
57 Assembly assembly
= null;
59 String name
= AssemblyUtils
.Normalize(args
.Name
);
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
);
74 UpdateCache(name
, assembly
);
79 // At last, we ask the user for the location
80 assembly
= ShowForm(args
, name
);
84 UpdateCache(name
, assembly
);
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
);
105 result
= form
.ShowDialog();
108 if (result
== DialogResult
.OK
)
110 lastAssemblyPath
= form
.AssemblyLocation
;
112 return form
.AssemblyLoaded
;
118 private void UpdateCache(string name
, Assembly loaded
)
120 name2Assembly
[name
] = loaded
;