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

前端 未结 5 1563
终归单人心
终归单人心 2020-12-20 14:05

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

相关标签:
5条回答
  • 2020-12-20 14:32

    It is also possible to get the DefaultNameSpace from the DTE. Very usefull if you are developing a Visual Studio Extension.

    EnvDTE.DTE dte = GetService(typeof(DTE)) as DTE;
    EnvDTE.Projects projects = dte?.Solution.Projects;
    if (projects != null)
    {
      foreach (EnvDTE.Project project in projects)
      {
        string defaultNameSpace1 = project.Properties.Item("DefaultNameSpace").Name;
        foreach (EnvDTE.ProjectItem projectItem in project.ProjectItems)
        {
          string defaultNameSpace2 = projectItem.Properties.Item("DefaultNameSpace").Name;
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-20 14:35

    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.

    0 讨论(0)
  • 2020-12-20 14:44

    You can't.

    The default namespace isn't stored anywhere within the assembly. It's just a project setting of Visual Studio.

    What I tend to do is use the name of the assembly: Assembly.GetName().Name. The problem with this is that it only works if the assembly name has not been changed.

    For your specific issue, you can use assembly.GetManifestResourceNames() and do some tests over those names, e.g.:

    string name = assembly.GetManifestResourceNames().Single(p => p.EndsWith(".SyntaxModes.Xml"))
    
    0 讨论(0)
  • 2020-12-20 14:45

    Not 100% sure it's what you're looking for but you can get the namespace using :

    this.GetType().Namespace
    
    0 讨论(0)
  • 2020-12-20 14:46

    I wrote an extension method for Assembly that just finds the resource by its filename within the included resources:

    public static Stream GetManifestResourceStreamByFileName(this Assembly assembly, string fileName)
    {
        var targetName = assembly.GetManifestResourceNames()
            .FirstOrDefault(x => x.EndsWith("." + fileName));
    
        return targetName == null ? null : assembly.GetManifestResourceStream(targetName);
    
    }
    

    then you just call like so:

        var assembly = Assembly.GetExecutingAssembly(); // or Assembly.GetAssembly(this.GetType()) or whatever
    
        var myResource = assembly.GetManifestResourceStreamByFileName("myResource.jpg");
    
    0 讨论(0)
提交回复
热议问题