Is there a way to get all namespaces you're 'using' within a class through C# code?

前端 未结 3 1854
北荒
北荒 2021-02-05 17:41

Is there any way to get a List which contains all \'usings\' within a namespace/class?

For instance

using System;
using System         


        
相关标签:
3条回答
  • 2021-02-05 17:58

    This will work for all types in methods of the declaring class, however it wont give all namespaces for all classes in the file where it was before compiling. That is impossible because after compilation the framework cannot know what was where in files.

    So if you have one CLASS per file this will work: If you are missing something (i look for fields and methods, maybe something is not taken in account, if that is so just add)

    List<string> namespaces = new List<string>();
    
            var m = MethodInfo.GetCurrentMethod();
    
                foreach (var mb in m.DeclaringType.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic))
                {
                    if (mb.MemberType == MemberTypes.Method && ((MethodBase)mb).GetMethodBody() != null)
                    {
    
                        foreach (var p in ((MethodInfo)mb).GetMethodBody().LocalVariables)
                        {
                            if (!namespaces.Contains(p.LocalType.Namespace))
                            {
                                namespaces.Add(p.LocalType.Namespace);
                                Console.WriteLine(p.LocalType.Namespace);
                            }
                        }
                    }
                    else if (mb.MemberType == MemberTypes.Field) {
                        string ns = ((System.Reflection.FieldInfo)mb).FieldType.Namespace;
                        if (!namespaces.Contains(ns))
                        {                        
                            namespaces.Add(ns);
                            Console.WriteLine(ns);
                        }
                    }
                }
    

    Sample output for my case:

    System
    System.Collections.Generic
    System.Reflection
    WindowsFormsApplication2
    System.Linq.Expressions
    
    0 讨论(0)
  • 2021-02-05 17:59

    You can use Mono Cecil to read a class definition from an assembly and get a list of all the referenced types in each method. From there, you can extract a list of namespaces (fully qualified type name minus the last part).

    0 讨论(0)
  • 2021-02-05 18:16

    Here's a rough untested attempt to get you started:

    IEnumerable<string> GetNamespacesUsed(string fileName)
    {
        var lines = System.IO.File.ReadAllLines(fileName);
        var usingLines = lines.Where(
            x => x.StartsWith("using ") && !x.StartsWith("using ("));
    
        foreach (var line in usingLines)
        {
            if (line.Contains("="))
                yield return line.Substring(line.IndexOf("="), line.Length - 1);
            else
                yield return line.Substring(line.Length - 1);
        }
    }
    

    The line.Length - 1 may not be correct to cut off the semicolon on the end. You'll need to test it to find out what it should be. Also, this assumes your code is formatted in a fairly standard way. If it's not, it won't work.

    0 讨论(0)
提交回复
热议问题