Browse Source

Add Import() extension method for INamespace

newNRvisualizers
Daniel Grunwald 13 years ago
parent
commit
ad6ce1059f
  1. 7
      ICSharpCode.NRefactory/TypeSystem/ICompilation.cs
  2. 5
      ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleCompilation.cs
  3. 24
      ICSharpCode.NRefactory/TypeSystem/TypeSystemExtensions.cs

7
ICSharpCode.NRefactory/TypeSystem/ICompilation.cs

@ -50,13 +50,18 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -50,13 +50,18 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// <summary>
/// Gets the root namespace of this compilation.
/// This is a merged .
/// This is a merged version of the root namespaces of all assemblies.
/// </summary>
INamespace RootNamespace { get; }
/// <summary>
/// Gets the root namespace for a given extern alias.
/// </summary>
/// <remarks>
/// If <paramref name="alias"/> is <c>null</c> or an empty string, this method
/// returns the global root namespace.
/// If alias with the specified name exists, this method returns null.
/// </remarks>
INamespace GetNamespaceForExternAlias(string alias);
IType FindType(KnownTypeCode typeCode);

5
ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleCompilation.cs

@ -120,6 +120,8 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -120,6 +120,8 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
protected virtual INamespace CreateRootNamespace()
{
// SimpleCompilation does not support extern aliases; but derived classes might.
// CreateRootNamespace() is virtual so that derived classes can change the global namespace.
INamespace[] namespaces = new INamespace[referencedAssemblies.Count + 1];
namespaces[0] = mainAssembly.RootNamespace;
for (int i = 0; i < referencedAssemblies.Count; i++) {
@ -134,6 +136,9 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -134,6 +136,9 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
public virtual INamespace GetNamespaceForExternAlias(string alias)
{
if (string.IsNullOrEmpty(alias))
return this.RootNamespace;
// SimpleCompilation does not support extern aliases; but derived classes might.
return null;
}

24
ICSharpCode.NRefactory/TypeSystem/TypeSystemExtensions.cs

@ -286,6 +286,30 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -286,6 +286,30 @@ namespace ICSharpCode.NRefactory.TypeSystem
{
return (IProperty)compilation.Import((IMember)property);
}
/// <summary>
/// Imports a namespace from another compilation.
/// </summary>
/// <remarks>
/// This method may return null if the namespace does not exist in the target compilation.
/// </remarks>
public static INamespace Import(this ICompilation compilation, INamespace ns)
{
if (compilation == null)
throw new ArgumentNullException("compilation");
if (ns == null)
return null;
if (ns.ParentNamespace == null) {
// root namespace
return compilation.GetNamespaceForExternAlias(ns.ExternAlias);
} else {
INamespace parent = Import(compilation, ns.ParentNamespace);
if (parent != null)
return parent.GetChildNamespace(ns.Name);
else
return null;
}
}
#endregion
#region GetDelegateInvokeMethod

Loading…
Cancel
Save