0

I want to get current namespace in a variable. I have the following piece of code.

Assembly assembly = Assembly.GetExecutingAssembly(); 
foreach(Type type in assembly.GetTypes()) 
{ 
    Console.WriteLine(type.Namespace); 
} 

By executing it, it show me the following:

System.Type[]
mynamespace
mynamespace
mynamespace
mynamespace
mynamespace.Properties
mynamespace
....

I also have this piece of code that return me the current namespace in string.

string name = MethodInfo.GetCurrentMethod().ReflectedType.Namespace;

What I want to do is to get the namespace, pass it to a variable. so the variable can use that namespace as reference to access the namespace methods and variables. basically I want to do something like this. I am looking for something like this:

this.GetCurrentNameSpace().variable1.ToString(); 

sounds funny but thats the kind of method/code I am looking for

How can I do it?

Emily Wong
  • 297
  • 3
  • 10
  • 1
    You have the namespace, so it's not clear what you're asking at all. Do you mean `var namespace = type.Namespace;`? – DavidG Feb 27 '19 at 10:20
  • @DavidG, I actually want to get the current namespace. then use it in other part of code. – Emily Wong Feb 27 '19 at 10:22
  • 2
    `string name = MethodInfo.GetCurrentMethod().ReflectedType.Namespace;` if *current namespace* means "namespace of currently executing code" – Dmitry Bychenko Feb 27 '19 at 10:23
  • @DmitryBychenko, Yes thanks, its working, but its in a string, how can I have it in a namespace? I want to use it to reference to other variables example namespacename.variable1.getData(). But i want to retrieve "namespacename" dynamically – Emily Wong Feb 27 '19 at 10:25
  • 1
    That's a very different question, and a really peculiar one too. This sounds like an XY problem now, and you'd probably have to use reflection to access those objects. – DavidG Feb 27 '19 at 10:27
  • @DavidG, I tried my best to explain, please modify question as you wish. – Emily Wong Feb 27 '19 at 10:27
  • It's not really possible to modify the question now. It's already been asked and answered in it's current state. – DavidG Feb 27 '19 at 10:29
  • what Dmitry Bychenko answered show me the current namespace, But I want to access to current namespace via variable. let me know if you have any idea. – Emily Wong Feb 27 '19 at 10:29
  • Let's, please, start from the requirements: what do you want to read/write/execute? E.g. in your comment `namespacename.variable1.getData()` is `variable1` some *class name* and `getData` some *static method name* – Dmitry Bychenko Feb 27 '19 at 10:30
  • I have a runtime compiler, that compile C# codes on the fly, the problem is the codes inside the compiler cant access the parent namespace that they are getting compiled and invoked. because they dont know the namespace name, currently i have to hardcode the namespace names in the C# codes so they can access other variables in the parent process. but I want to get that namespace name dynamically during run time. sorry i simplified things, hard to explain. – Emily Wong Feb 27 '19 at 10:33
  • i just came up with an example, it should be something like this. namespacename.variable1.ToString(); but i dont have the "namespacename" and i want it to be the current namespace that the code is being executed. – Emily Wong Feb 27 '19 at 10:36
  • I am looking for something like this.GetCurrentNameSpace().variable1.ToString(); sounds funny but thats the kind of method/code i am looking for – Emily Wong Feb 27 '19 at 10:38
  • This sounds pretty awful if I'm honest. Why not pass in the objects that are needed to the compiled code? That way they don't care about namespaces. – DavidG Feb 27 '19 at 10:45
  • @DavidG, I dont know what objects need to be passed in future... – Emily Wong Feb 27 '19 at 10:46
  • That makes even less sense! – DavidG Feb 27 '19 at 10:47
  • @david, I am coding a uncommon and special application.. – Emily Wong Feb 27 '19 at 10:48
  • Well clearly haha. But it still makes no sense that you don't know what the compiled code is going to access. From a security standpoint alone, it's scary to let it run wild and call anything it wants. But why not let the compiled code reference a shared library, and then it can call everything in there? – DavidG Feb 27 '19 at 10:50
  • The namespace name and all the symbols will be obfuscated, nothing is what i know prior, so i need to get them after execution, whats why i want to know how to get the namespace so from there i can dig in and find other method name in there.. – Emily Wong Feb 27 '19 at 10:52

0 Answers0