Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get the default namespace of project csproj (VS 2008)

I have VS 2008 and csproj project (C# Library).

In properties of project, has an assembly name, and default namespace. Note: each class has a namespace.

Is it possible, in runtime, get the value of default namespace ??

My target is use resources, and I need default namespace's value:

 Assembly assembly = Assembly.GetExecutingAssembly();

 //foreach (string resourceName in assembly.GetManifestResourceNames()){}

 Stream syntaxModeStream = assembly.GetManifestResourceStream(pathToResources
+ ".SyntaxModes.xml");

Update:

Pieter said I can't. Default namespace don't stored in assembly

var resource = assembly.GetManifestResourceNames().Single(p => p.EndsWith(".SyntaxModes.Xml"))

where is the default namespace stored ??

Only can I read using Addins Visual Studio (DTE object) ??

like image 594
Kiquenet Avatar asked Feb 03 '11 12:02

Kiquenet


People also ask

What is default namespace in Visual Studio?

Default namespace is the namespace that Visual studio sets when you create a new class. Next time you do Right Click > Add > Class it would use the namespace you specified in the above step.

What is the default namespace in C#?

global is the default namespace.

How do I change a project namespace?

Place your cursor in the namespace name. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Change namespace to <folder name>.

How do I import a namespace in Visual Studio?

To add an imported namespaceIn Solution Explorer, double-click the My Project node for the project. In the Project Designer, click the References tab. In the Imported Namespaces list, select the check box for the namespace that you wish to add. In order to be imported, the namespace must be in a referenced component.


1 Answers

I came to this page in search of confirmation that I hadn't overlooked anything in my search for an assembly property that corresponds to the root namespace entered into the main Visual Studio project property page of a Windows application.

Since I had an idea for solving the problem for the use case that brought me to this page, and I anticipate that at least a few others would benefit from my discovery, I kept the page open while I conducted my research.

As I suspected, it is relatively easy to associate a meaningful, not to mention ultra useful, namespace with the entry assembly. The following long expression returns the namespace of the Program class defined in the Program.cs module of any Windows application:

System.Reflection.Assembly.GetEntryAssembly().EntryPoint.DeclaringType.Namespace

The following C# statement, executed from anywhere in the application domain, even through a method call that reaches into a DLL, reports the name of the entry point routine and its namespace.

Console.WriteLine (
    "Namespace of Entry Point Routine {0} = {1} " ,
    System.Reflection.MethodBase.GetCurrentMethod ( ).Name ,
    System.Reflection.Assembly.GetEntryAssembly ( ).EntryPoint.DeclaringType.Namespace );

For a console mode program called OperatingParameters_Demo.exe, whose root namespace is OperatingParameters_Demo, the above statement yields the following output.

Namespace of Entry Point Routine Main = OperatingParameters_Demo

The practical use of this expression is that you can use it to construct the absolute (fully qualified) name of the Properties.Settings and Properties.Resources namespaces of the entry assembly.

  1. Access to the settings from anywhere in the application means that you can search for a setting, the name of which is unknown until run time.
  2. Access to the resources from anywhere in the application means that string resources that are stored in the entry assembly or its satellite resource assemblies are accessible from throughout the application domain, even if their names are unknown until run time.

I am almost finished with an application that takes advantage of this technique to display parameter names that are stored in resource strings based on resource names that are derived from the parameter's internal name. This lies at the heart of the generic program parameter processing library that the tool demonstrates.

This demonstration project began when I started work on a programmer's tool to import C/C++ header files into a project directory, so that I can deploy a self-contained project into GitHub, even though the master copies of the headers are stored elsewhere because they are shared by dozens of projects. As its parameter parsing engine neared completion, I realized that I was incredibly close to having the generic operating parameter parser and backing store that I have wanted for many years. The C/C++ header importer will eventually be published. However, at the moment, only its parsing engine is finished. Since it is useful on its own, I intend to publish it first. Stay tuned to The Code Project and GitHub.

like image 85
David A. Gray Avatar answered Sep 19 '22 17:09

David A. Gray