我需要在一个纯编译的dll中嵌入一些资源,这个dll是用php编写的。这些是我在visual studio中设置为"Embedded Resource“的txt文件。
我的问题是,我不能通过GetManifestResourceStream使用Assembly类来获取资源。
我尝试了这样的代码:使用System\Reflection\Assembly
$asm = Assembly::GetExecutingAssembly(); //this gives me mscorlib instead of my dll
$str = $asm->GetManifestResourceStream("name");我的问题是:如何访问phalanger中的嵌入式资源?非常感谢
发布于 2012-05-30 16:11:58
我不确定,为什么Assembly::GetExecutingAssembly()返回不正确的值。无论如何,要解决$asm值的问题,请使用以下代码:
$MyType = CLRTypeOf MyProgram;
$asm = $MyType->Assembly;然后,您可以在发布时访问嵌入的资源
$asm->GetManifestResourceStream("TextFile1.txt");或者,您可以将标准资源文件(.resx)包含到项目中,并使用\System\Resources\ResourceManager
$this->manager = new \System\Resources\ResourceManager("",$asm);
$this->manager->GetObject("String1",null);请注意,目前在Phalanger项目中只能有一个.resx
发布于 2016-01-04 03:27:26
这个问题很老了,但是Phalanger代码中负责这个问题的部分(Php.Core.Emit.AddResourceFile()方法)并没有因为这个问题而改变。我遇到了同样的问题,并以(几乎)非破解的方式解决了它。不过,您必须提供替代名称(/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() (就像我认为它在提供无法识别的文件格式时应该使用的那样)。它使用返回ResourceWriter的ModuleBuilder.DefineResource(),这只适用于.resources文件。这就是当您需要读取资源时使用ResourceReader的要求。
备注:此答案适用于Phalanger master branch at the time of writing及自2011年左右开始的早期版本。注意,因为它看起来像一个bug (特别是需要同时使用原始名称和备用名称)。
https://stackoverflow.com/questions/10796151
复制相似问题