有没有办法获取您正在“使用”的所有命名空间?通过 C# 代码在类中?
有没有办法获得一个包含命名空间/类中所有“使用”的 List
?
例如,
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”、“System.Text”、“System.Reflection”等的列表。
编辑:正如下面评论的那样, 我正在使用这个 http://kamimucode.com/Home.aspx/C- Sharp-Eval/1 - C# 评估器,我必须向 TypeParser 提供命名空间列表。问题是,我并不总是知道该放哪些。我正在使用 C# 开发一种简单的脚本语言,我也可以从中调用 C# 代码。我的另一个选择是在脚本语言中创建一个关键字,例如“using”,但我真的不想这样做。我希望能够选择一个 C# 对象,通过 script.txt 对其执行任何我想要的操作,并可以自由地使用其引用的命名空间。
我想在我的 Xna 游戏引擎中使用它,以便能够编写可以执行 C# 的自定义脚本以及我的简单脚本函数。
Is there any way to get a List<string>
which contains all 'usings' within a namespace/class?
For instance
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
{
I would have a List with "System","System.Text", "System.Reflection", and so on.
EDIT: 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这适用于声明类的方法中的所有类型,但是它不会为编译前文件中的所有类提供所有命名空间。这是不可能的,因为编译后框架无法知道文件中的内容。
因此,如果每个文件有一个类,则这将起作用:
如果您缺少某些内容(我寻找字段和方法,也许没有考虑某些内容,如果是这样,只需添加)
我的案例的示例输出:
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)
Sample output for my case:
这是一个未经测试的粗略尝试,可以帮助您入门:
line.Length - 1
可能无法正确截断末尾的分号。您需要对其进行测试以找出它应该是什么。另外,这假设您的代码以相当标准的方式格式化。如果不是,那就行不通。Here's a rough untested attempt to get you started:
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.您可以使用 Mono Cecil 从程序集中读取类定义并获取所有引用的列表每个方法中的类型。从那里,您可以提取命名空间列表(完全限定的类型名称减去最后一部分)。
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).