gpt4 book ai didi

.net - 如何获得程序集的根命名空间?

转载 作者:行者123 更新时间:2023-12-04 16:57:11 25 4
gpt4 key购买 nike

给定一个 System.Reflection.Assembly 的实例.

最佳答案

当我想通过其 list 资源流从当前程序集中加载资源时,我多次遇到过这种困境。

事实是,如果您使用 Visual Studio 将文件作为资源嵌入到程序集中,则其 list 资源名称将从 Visual Studio 项目中定义的程序集的默认命名空间派生。

我想出的最佳解决方案(避免将默认命名空间硬编码为某个字符串)是简单地确保您的资源加载代码始终发生在同样位于默认命名空间中的类内部,然后是以下近乎通用的方法可能用过了。

此示例正在加载嵌入式架构。

XmlSchema mySchema;
string resourceName = "MyEmbeddedSchema.xsd";
string resourcesFolderName = "Serialisation";
string manifestResourceName = string.Format("{0}.{1}.{2}",
this.GetType().Namespace, resourcesFolderName, resourceName);
using (Stream schemaStream = currentAssembly.GetManifestResourceStream(manifestResourceName))
mySchema = XmlSchema.Read(schemaStream, errorHandler);

另见: How to get Namespace of an Assembly?

编辑:还注意到我在 http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/3a469f5d-8f55-4b25-ac25-4778f260bb7e 回答的问题的非常详细的答案

另一个编辑以防有同样问题的人来找:解决资源加载问题的好主意: How get the default namespace of project csproj (VS 2008)

关于.net - 如何获得程序集的根命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52042035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com