简体   繁体   中英

Get all namespaces associated with a Type?

Is there a way to get all the namespaces associated with a class/type using Reflection?

For example, assuming my variable's is:

var list = List<MyClass> 

and my class is defined as

public class MyClass
{
   property int32 MyNumber {get; set;}
}

I would like to be able to get the following namespaces returned:

  • MyCompany.MyClass (as it's being used my MyClass)
  • System.Collections.Generic (as it's being used by List)
  • System as int32 (as it's being used by MyNumber)

Thanks.

You can parse the type using reflecion.

You will get many more results than expected because of the framework design.

I hope I haven't made too many mistakes.

The test

namespace ConsoleApp
{
  public class Program
  {
    static public void Main(string[] args)
    {
      var list = new List<MyClass>();
      var typeSearched = list.GetType();
      Console.WriteLine($"{typeSearched.Name} needs these namespaces:");
      foreach ( var item in typeSearched.GetUsingNamespaces().OrderBy(kvp => kvp.Key) )
      {
        string names = string.Join(Environment.NewLine + "    ",
                                   item.Value.Select(t => t.Name)
                                             .OrderBy(n => n));
        Console.WriteLine($"# {item.Key} for type(s):" + Environment.NewLine +
                          $"    {names}");
        Console.WriteLine();
    }
  }
  public class MyClass
  {
    public Int32 MyNumber {get; set;}
  }
}

The reflexion helper class

using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;

namespace ConsoleApp
{
  static public class ReflexionHelper
  {
    // See below
  }
}

Tools methods

static public void AddNoDuplicates<T>(this IList<T> list, T value)
{
  if ( !list.Contains(value) ) list.Add(value);
}

static public void AddNoDuplicates<TKey, TValue>(this IDictionary<TKey, List<TValue>> list,
                                                 TKey key, TValue value)
{
  if ( key == null ) return;
  if ( !list.ContainsKey(key) )
    list.Add(key, new List<TValue>() { value });
  else
  if ( !list[key].Contains(value) )
    list[key].Add(value);
}

static public void AddRangeNoDuplicates<T>(this IList<T> list, IEnumerable<T> collection)
{
  foreach ( T value in collection )
    if ( !list.Contains(value) )
      list.Add(value);
}

The method to parse the type

static public Dictionary<string, List<Type>> GetUsingNamespaces(this Type typeSearched)
{
  var result = new Dictionary<string, List<Type>>();
  result.AddNoDuplicates(typeSearched.Namespace, typeSearched);
  foreach ( Type type in typeSearched.GetMembersTypes() )
  {
    result.AddNoDuplicates(type.Namespace, type);
    foreach ( var implement in type.GetInterfaces() )
      result.AddNoDuplicates(implement.Namespace, implement);
    foreach ( var argument in type.GetGenericArguments() )
      result.AddNoDuplicates(argument.Namespace, argument);
  }
  return result;
}

The method to get all members of a type

static public List<Type> GetMembersTypes(this Type type)
{
  var flags = BindingFlags.Static
            | BindingFlags.Instance
            | BindingFlags.Public
            | BindingFlags.NonPublic;
  var result = new List<Type>();
  foreach ( var field in type.GetFields(flags) )
    result.AddNoDuplicates(field.FieldType);
  foreach ( var property in type.GetProperties(flags) )
    result.AddNoDuplicates(property.PropertyType);
  foreach ( var ev in type.GetEvents(flags) )
    result.AddNoDuplicates(ev.EventHandlerType);
  foreach ( var method in type.GetMethods(flags) )
  {
    result.AddNoDuplicates(method.ReturnType);
    foreach ( var a in method.GetParameters() )
      result.AddNoDuplicates(a.ParameterType);
  }
  foreach ( var constructor in type.GetConstructors() )
    foreach ( var param in constructor.GetParameters() )
      result.AddNoDuplicates(param.ParameterType);
  foreach ( var nested in type.GetNestedTypes(flags) )
    result.AddRangeNoDuplicates(GetMembersTypes(nested));
  return result;
}

Future improvements

Analyze attributes and other things forgotten.

Test output short

ConsoleApp
System
System.Collections
System.Collections.Generic
System.Collections.ObjectModel
System.Reflection
System.Runtime.InteropServices
System.Runtime.Serialization

Test output full

# ConsoleApp for type(s):
    MyClass
    MyClass[]

# System for type(s):
    Action`1
    Array
    Boolean
    Comparison`1
    Converter`2
    ICloneable
    IComparable
    IComparable`1
    IComparable`1
    IComparable`1
    IConvertible
    IDisposable
    IEquatable`1
    IEquatable`1
    IEquatable`1
    IFormattable
    Int32
    Object
    Predicate`1
    String
    Type
    Void

# System.Collections for type(s):
    ICollection
    IEnumerable
    IEnumerator
    IList
    IStructuralComparable
    IStructuralEquatable

# System.Collections.Generic for type(s):
    Enumerator
    ICollection`1
    ICollection`1
    ICollection`1
    ICollection`1
    IComparer`1
    IEnumerable`1
    IEnumerable`1
    IEnumerable`1
    IEnumerable`1
    IEnumerable`1
    IEnumerator`1
    IEnumerator`1
    IList`1
    IList`1
    IList`1
    IList`1
    IReadOnlyCollection`1
    IReadOnlyCollection`1
    IReadOnlyCollection`1
    IReadOnlyCollection`1
    IReadOnlyList`1
    IReadOnlyList`1
    IReadOnlyList`1
    IReadOnlyList`1
    List`1
    List`1
    List`1
    List`1
    T
    T
    T[]
    TOutput

# System.Collections.ObjectModel for type(s):
    ReadOnlyCollection`1

# System.Reflection for type(s):
    ICustomAttributeProvider
    IReflect

# System.Runtime.InteropServices for type(s):
    _MemberInfo
    _Type

# System.Runtime.Serialization for type(s):
    ISerializable

Some type names are duplicated but the Type instance is different...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM