Skip to content

Commit 84164b9

Browse files
committed
Patch submitted by Ofer to resolve sort of type issues when models composed models are in separate dlls. Closes issue http://nmodel.codeplex.com/WorkItem/View.aspx?WorkItemId=3255
1 parent c2a9bec commit 84164b9

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/NModel/Modeling/Internals/InterpretationContext.cs

+47-3
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ public bool SortTypeTryGetValue(Symbol sort, out Type/*?*/ t)
185185
/// Get the default type of the sort
186186
/// </summary>
187187
public Type DefaultSortType(Symbol sort)
188-
{
188+
{
189189
Type result;
190190
if (sortType.TryGetValue(sort, out result))
191191
return result;
192192

193-
string[] namespaces = new string[] {"NModel", "NModel.Internals", "NModel.Terms", "System" };
193+
string[] namespaces = new string[] { "NModel", "NModel.Internals", "NModel.Terms", "System" };
194194

195195
if (sort.DomainParameters != null && sort.DomainParameters.Count > 0)
196196
{
@@ -241,15 +241,59 @@ public Type DefaultSortType(Symbol sort)
241241
break;
242242
}
243243
}
244+
if (result == null)
245+
// If the execution got here it means that it didn't find the type of sort
246+
// in this model and in the NModel framework.
247+
// Check for the type in the AppDomain assemblies -
248+
// Is it a type of another model program that is being composed with this one
249+
result = getSortTypeFromAppDomainAssemblies(namespaces, sort.Name);
244250

245251
if (result == null)
246-
throw new ArgumentException("No default type for sort " + sort.ToString());
252+
throw new ArgumentException("No default type for sort " + sort.ToString());
247253

248254
this.RegisterSortType(sort, result);
249255
return result;
250256
}
251257

258+
/// <summary>
259+
/// Check for the type in the AppDomain assemblies -
260+
/// Is it a type of another model program that is being composed with this one
261+
/// Exclude from checking all the namespaces that have been checked already
262+
/// </summary>
263+
/// <param name="excludeNameSpaces"></param>
264+
/// <param name="sortTypeName"></param>
265+
/// <returns>Type - the type of the given sort, or null if a matching type was not found</returns>
266+
private Type getSortTypeFromAppDomainAssemblies(string[] excludeNameSpaces, string sortTypeName)
267+
{
268+
Type thisSortType = null;
252269

270+
// Get all the assemblies loaded into this AppDomain.
271+
foreach (Assembly thisAssembly in AppDomain.CurrentDomain.GetAssemblies())
272+
{
273+
int i;
274+
// Exclude all the namespaces that have been checked already
275+
for (i = 0; i < excludeNameSpaces.Length; ++i)
276+
{
277+
if (thisAssembly.GetName().Name == excludeNameSpaces[i])
278+
break;
279+
}
280+
if (i == excludeNameSpaces.Length)
281+
{
282+
foreach (Type t in thisAssembly.GetExportedTypes())
283+
{
284+
if (t.Name == sortTypeName)
285+
{
286+
// Found it!
287+
thisSortType = t;
288+
break;
289+
}
290+
}
291+
}
292+
if (thisSortType != null)
293+
break;
294+
}
295+
return thisSortType;
296+
}
253297

254298
///// <summary>
255299
///// Accesses the mapping of .NET types to sorts (abstract types used to connect model programs). This

0 commit comments

Comments
 (0)