我遇到GetManifestResourceStream
为嵌入式资源返回null的情况,其中资源名称是针对GetManifestResourceNames
进行验证的。
Build Action
是“嵌入资源”。GetManifestResourceStream
的资源名称存在于GetManifestResourceNames
返回的列表中。传递给GetManifestResourceStream
的资源名称来自GetManifestResourceNames
返回的列表。我的步骤是:
获取合格的资源路径。
private string GetCountryResourcePath(string countryName)
{
Assembly assembly = this.GetType().GetTypeInfo().Assembly;
string[] assemblyResources = assembly.GetManifestResourceNames();
string searchTerm = string.Format(".{0}, countryName);
foreach(string resourcePath in assemblyResources)
{
if (resourcePath.LastIndexOf(searchTerm, StringComparison.OrdinalIgnoreCase)>0)
return resourcePath;
}
return string.Empty;
}
如果找到指定国家/地区的资源路径,请尝试使用GetManifestResourceStream
打开:
string countryResourcePath = GetCountryResourcePath( countryName );
if (countryResourcePath == String.Empty)
return;
using (Stream countryStream = assembly.GetManifestResourceStream(countryStream))
...
就我而言,使用countryName:“Russia.json”。
GetCountryResourcePath("Russia")
返回“Jfx.Data.Countries.Russia.json”。
GetManifestResourceStream("Jfx.Data.Countries.Russia.json")
返回null。