C# 为什么在调用 GetManifestResourceNames 时,资源名称存在时 GetManifestResourceStream 返回 null?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10726857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:00:26  来源:igfitidea点击:

Why does GetManifestResourceStream returns null while the resource name exists when calling GetManifestResourceNames?

c#asp.net

提问by MsBugKiller

I have a web application project. I generated the DLL and import it in another project. I implemented VirtualPathProvider.

我有一个 Web 应用程序项目。我生成了 DLL 并将其导入到另一个项目中。我实施了VirtualPathProvider.

I followed this web site: http://support.microsoft.com/kb/910441/en-us?spid=8940&sid=global, and everything works until I add another site master.

我关注了这个网站:http: //support.microsoft.com/kb/910441/en-us?spid=8940&sid=global,一切正常,直到我添加另一个站点管理员。

  1. I added site_export.masterand changed its Build Action to Embedded Resource.
  2. I changed my page to use the new site master.
  3. GetManifestResourceStream()returns nullwhen I load site_export.master.
  4. I call GetManifestResourceNames()to check if site_export.masterexists in the DLL and it does. It's in the list. All of the name spaces match. I didn't list the name space out here.
  1. 我添加site_export.master并将其构建操作更改为嵌入式资源。
  2. 我更改了我的页面以使用新的站点主站。
  3. GetManifestResourceStream()null当我加载时返回site_export.master
  4. 我打电话GetManifestResourceNames()检查site_export.masterDLL中是否存在,它确实存在。它在列表中。所有的名称空间都匹配。我没有在这里列出名称空间。

Why can't GetManifestResourceStream()load my new site_export.master? It loads site.masterjust fine. I know my DLL is loaded because I can see other files in the DLL.

为什么不能GetManifestResourceStream()加载我的新site_export.master?它加载site.master得很好。我知道我的 DLL 已加载,因为我可以看到 DLL 中的其他文件。

回答by Shahdat

Remember the following issues...

请记住以下问题...

Step 1. Build action set to embedded resource see

第 1 步。将构建操作设置为嵌入资源请参见

C#'s GetManifestResourceStream Gotcha

C# 的 GetManifestResourceStream 陷阱

Step 2. Use namespace.resourcenamesee GetManifestResourceStream() returns null ?

第 2 步。使用namespace.resourcename请参阅GetManifestResourceStream() 返回 null ?

Actually, this method returns null if a private resource in another assembly is accessed and the caller does not have ReflectionPermissionwith the ReflectionPermissionFlag.MemberAccessflag.

实际上,如果访问另一个程序集中的私有资源并且调用者没有ReflectionPermissionReflectionPermissionFlag.MemberAccess标志,则此方法返回 null 。

回答by Yury Schkatula

Side-hint. To make sure you're in the right assembly and with right name: dump and evaluate all the resources available in your target assembly

侧面提示。确保您在正确的程序集中并使用正确的名称:转储并评估目标程序集中的所有可用资源

string[] names = assembly.GetManifestResourceNames();

(in my case, I misused a namespace from another assembly)

(就我而言,我误用了另一个程序集中的命名空间)

回答by Rodrigo T.

Try this:

尝试这个:

Dim ctx As Windows.ApplicationModel.Resources.Core.ResourceContext = New Windows.ApplicationModel.Resources.Core.ResourceContext()
ctx.Languages = {Globalization.CultureInfo.CurrentUICulture.Name}
Dim rmap As Windows.ApplicationModel.Resources.Core.ResourceMap = Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap
Dim res = rmap.GetValue("Assets/sample.png", ctx)
Dim resFile = Await res.GetValueAsFileAsync

The Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMaplist all resources.

Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap列表中的所有资源。

回答by RajivS

Hint and Caution: If the "Assembly name" and "Default namespace" does not match in the project file then also GetManifestResourceStream would return null. GetManifestResourceNames still returns the names of assemblies but during loading the manifest would not work.

提示和警告:如果项目文件中的“程序集名称”和“默认命名空间”不匹配,则 GetManifestResourceStream 也将返回 null。GetManifestResourceNames 仍然返回程序集的名称,但在加载期间清单将不起作用。

回答by Deepak Chaudhary

I did this to make it work:

我这样做是为了让它工作:

Step 1:Select the resource (CRDF.xsl in my case) and right click > Properties. Set Build Action to "EmbeddedResource" and Logical Name to name of your choice, e.g. CRDFXSL.

步骤 1:选择资源(在我的例子中为 CRDF.xsl)并右键单击 > 属性。将 Build Action 设置为“EmbeddedResource”,将 Logical Name 设置为您选择的名称,例如 CRDFXSL。

Step 2 :Change your Source code like this:

第 2 步:像这样更改源代码:

Assembly _assembly;
  _assembly = Assembly.GetExecutingAssembly();         
  xslStream = _assembly.GetManifestResourceStream("CRDFXSL");
  _xmlReader = XmlReader.Create(xslStream);

Thus everything went smoooooooth..

因此,一切顺利..