如何获取项目csproj的默认命名空间(VS 2008)

发布于 2024-10-16 02:39:00 字数 641 浏览 4 评论 0原文

我有 VS 2008 和 csproj 项目(C# 库)。

在项目的属性中,具有程序集名称和默认命名空间。 注意:每个类都有一个命名空间。

是否有可能在运行时获取默认命名空间的值?

我的目标是使用资源,我需要默认命名空间的值:

 Assembly assembly = Assembly.GetExecutingAssembly();

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

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

更新:

Pieter 说我不能。默认命名空间不存储在程序集中

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

默认命名空间存储在哪里?

我只能使用 Addins Visual Studio(DTE 对象)读取?

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) ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

稳稳的幸福 2024-10-23 02:39:00

不能。

默认命名空间不存储在程序集中的任何位置。这只是Visual Studio的一个项目设置。

我倾向于使用程序集的名称:Assembly.GetName().Name。这样做的问题是,它仅在程序集名称未更改的情况下才有效。

对于您的具体问题,您可以使用 assembly.GetManifestResourceNames() 并对这些名称进行一些测试,例如:

string name = assembly.GetManifestResourceNames().Single(p => p.EndsWith(".SyntaxModes.Xml"))

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"))
溺深海 2024-10-23 02:39:00

不能 100% 确定这就是您正在寻找的内容,但您可以使用以下命令获取命名空间:

this.GetType().Namespace

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

this.GetType().Namespace
你对谁都笑 2024-10-23 02:39:00

我来到此页面是为了确认我在搜索程序集属性时没有忽略任何内容,该属性与输入到 Windows 应用程序的主 Visual Studio 项目属性页中的根命名空间相对应。

由于我有一个解决用例问题的想法,并将我带到此页面,并且我预计至少其他一些人会从我的发现中受益,因此我在进行研究时保持页面打开。

正如我所怀疑的,将一个有意义的(更不用说超级有用)命名空间与入口程序集关联起来相对容易。以下长表达式返回在任何 Windows 应用程序的 Program.cs 模块中定义的 Program 类的命名空间:

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

以下 C# 语句从应用程序域中的任何位置执行,即使通过到达 DLL 的方法调用,也可以报告入口点例程的名称及其命名空间。

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

对于名为 OperatingParameters_Demo.exe 的控制台模式程序(其根命名空间为 OperatingParameters_Demo),上述语句将产生以下输出。

Namespace of Entry Point Routine Main = OperatingParameters_Demo

此表达式的实际用途是,您可以使用它来构造条目程序集的 Properties.SettingsProperties.Resources 命名空间的绝对(完全限定)名称。

  1. 从应用程序中的任何位置访问设置意味着您可以搜索设置,其名称在运行时之前是未知的。
  2. 从应用程序中的任何位置访问资源意味着可以从整个应用程序域访问存储在条目程序集或其附属资源程序集中的字符串资源,即使它们的名称在运行时之前未知。

我几乎完成了一个应用程序,该应用程序利用此技术来显示存储在资源字符串中的参数名称,这些参数名称基于从参数的内部名称派生的资源名称。这是该工具演示的通用程序参数处理库的核心。

这个演示项目开始于我开始使用程序员工具将 C/C++ 头文件导入到项目目录中,这样我就可以将一个独立的项目部署到 GitHub 中,尽管头文件的主副本存储在其他地方,因为它们被数十个项目共享。随着其参数解析引擎接近完成,我意识到我非常接近拥有我多年来一直想要的通用操作参数解析器和后备存储。 C/C++ 标头导入器最终将发布。然而,目前只完成了它的解析引擎。由于它本身很有用,所以我打算先发布它。请继续关注 The Code ProjectGitHub

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.

谁对谁错谁最难过 2024-10-23 02:39:00

我为 Assembly 编写了一个扩展方法,它只是通过包含的资源中的文件名查找资源:

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);

}

然后您只需像这样调用:

    var assembly = Assembly.GetExecutingAssembly(); // or Assembly.GetAssembly(this.GetType()) or whatever

    var myResource = assembly.GetManifestResourceStreamByFileName("myResource.jpg");

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");
入怼 2024-10-23 02:39:00

还可以从 DTE 获取 DefaultNameSpace。如果您正在开发 Visual Studio 扩展,则非常有用。

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;
    }
  }
}

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;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文