有没有办法让你'使用',通过C#代码的类中的所有命名空间? [英] Is there a way to get all namespaces you're 'using' within a class through C# code?

查看:128
本文介绍了有没有办法让你'使用',通过C#代码的类中的所有命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法让一个列表<串> ,其中包含一个命名空间/类内的所有usings

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

例如

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text.RegularExpressions;
using System.Reflection;
namespace MyNamespace.Other.Scripting
{



我有一个列表系统,System.Text,的System.Reflection,等等。

I would have a List with "System","System.Text", "System.Reflection", and so on.

编辑:作为下面评论说:$我使用这个 http://kamimucode.com/Home.aspx / C锋利评估和演示/ 1 - C#计算器,和我有一个命名空间的列表喂TypeParser。事情是,我不会总是知道把哪些。我工作在C#中一个简单的脚本语言,并从它我就可以调用C#代码以及。我的另一种方法是创建一个关键字,如使用的脚本语言,但我真的不想这样做。我希望能挑到一个C#对象,做任何我想做的事情通过我的script.txt,并自由地使用它引用的命名空间。

As a commented below, I'm using this http://kamimucode.com/Home.aspx/C-sharp-Eval/1 - C# evaluator, and I have to feed the TypeParser with a list of namespaces. The thing is, I won't always know which ones to put. I'm working on a simple scripting language in C# and from it I'll be able to call C# code aswell. My other alternative is create a keyword such as "using" in the scripting language, but I really don't want to do that. I want to be able to pick an C# Object, do whatever I want with it through my script.txt, and be free to use its referenced namespaces.

我要用它在我的XNA游戏引擎,能写能与我的简单的脚本功能一起执行C#自定义脚本。

I want to use it in my Xna Game engine, to be able to write custom scripts which can execute C# along with my simple script functions.

推荐答案

这将在声明类的方法,所有类型的工作,但是它不会让所有命名空间的地方是在编译之前文件中的所有类。 ,这是不可能的,因为编译后的框架可以不知道什么是其中文件

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.

所以,如果你有每个文件一个CLASS这将工作:
。如果你是失去了一些东西(我找字段和方法,或许真的不采取帐户,如果是这样的话只需添加)

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

这篇关于有没有办法让你'使用',通过C#代码的类中的所有命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆