如何获取项目csproj的默认命名空间(VS 2008)
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不能。
默认命名空间不存储在程序集中的任何位置。这只是Visual Studio的一个项目设置。
我倾向于使用程序集的名称:
Assembly.GetName().Name
。这样做的问题是,它仅在程序集名称未更改的情况下才有效。对于您的具体问题,您可以使用 assembly.GetManifestResourceNames() 并对这些名称进行一些测试,例如:
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.:不能 100% 确定这就是您正在寻找的内容,但您可以使用以下命令获取命名空间:
Not 100% sure it's what you're looking for but you can get the namespace using :
我来到此页面是为了确认我在搜索程序集属性时没有忽略任何内容,该属性与输入到 Windows 应用程序的主 Visual Studio 项目属性页中的根命名空间相对应。
由于我有一个解决用例问题的想法,并将我带到此页面,并且我预计至少其他一些人会从我的发现中受益,因此我在进行研究时保持页面打开。
正如我所怀疑的,将一个有意义的(更不用说超级有用)命名空间与入口程序集关联起来相对容易。以下长表达式返回在任何 Windows 应用程序的 Program.cs 模块中定义的 Program 类的命名空间:
以下 C# 语句从应用程序域中的任何位置执行,即使通过到达 DLL 的方法调用,也可以报告入口点例程的名称及其命名空间。
对于名为 OperatingParameters_Demo.exe 的控制台模式程序(其根命名空间为 OperatingParameters_Demo),上述语句将产生以下输出。
此表达式的实际用途是,您可以使用它来构造条目程序集的 Properties.Settings 和 Properties.Resources 命名空间的绝对(完全限定)名称。
我几乎完成了一个应用程序,该应用程序利用此技术来显示存储在资源字符串中的参数名称,这些参数名称基于从参数的内部名称派生的资源名称。这是该工具演示的通用程序参数处理库的核心。
这个演示项目开始于我开始使用程序员工具将 C/C++ 头文件导入到项目目录中,这样我就可以将一个独立的项目部署到 GitHub 中,尽管头文件的主副本存储在其他地方,因为它们被数十个项目共享。随着其参数解析引擎接近完成,我意识到我非常接近拥有我多年来一直想要的通用操作参数解析器和后备存储。 C/C++ 标头导入器最终将发布。然而,目前只完成了它的解析引擎。由于它本身很有用,所以我打算先发布它。请继续关注 The Code Project 和 GitHub。
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:
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.
For a console mode program called OperatingParameters_Demo.exe, whose root namespace is OperatingParameters_Demo, the above statement yields the following output.
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.
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.
我为 Assembly 编写了一个扩展方法,它只是通过包含的资源中的文件名查找资源:
然后您只需像这样调用:
I wrote an extension method for Assembly that just finds the resource by its filename within the included resources:
then you just call like so:
还可以从 DTE 获取 DefaultNameSpace。如果您正在开发 Visual Studio 扩展,则非常有用。
It is also possible to get the DefaultNameSpace from the DTE. Very usefull if you are developing a Visual Studio Extension.