这个问题很老,但是Php.Core.Emit.AddResourceFile()
自从提出这个问题以来,负责这个问题的 Phalanger 代码(方法)部分没有改变。我遇到了同样的问题并以(几乎)非hacky的方式解决了它。您必须提供替代名称 ( /res:/path/to/filename,alternative-name
) 才能使其正常工作。
$asm = clr_typeof('self')->Assembly;
$resourceStream = $asm->GetManifestResourceStream("filename");
$reader = new \System\Resources\ResourceReader($resourceStream);
$type = $data = null;
$reader->GetResourceData("alternative-name", $type, $data);
// and still there are 4 excess bytes
// representing the length of the resource
$data = \substr($data, 4);
$stream = new IO\MemoryStream($data);
// after this $stream is usable as you would expect
直截了当GetManifestResourceStream()
(正如 Jakub 所建议的那样)不起作用,因为 Phalanger 不使用System.Reflection.Emit.ModuleBuilder.DefineManifestResource()
(就像我认为在提供无法识别的文件格式时应该使用的那样)。它使用ModuleBuilder.DefineResource()
which 返回ResourceWriter
,它只适合.resources
文件。这就是ResourceReader
您需要阅读资源时使用的要求。
Note: This answer applies to Phalanger master branch at the time of writing and prior versions since circa 2011. Noted because it looks like a bug (especially the need to use both original and alternative names).