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.
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:
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).
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.