English 中文(简体)
记忆犹新,而不是作为资源添加的档案。
原标题:MemoryStream instead of byte[] for files added as resources?

我拥有一个“网络”大会,它把一些文件作为资源(每份200KB)。 我以前曾使用<代码>查阅过这些编号。 ResourceManager.GetObject() methods on the auto- produced Resources category, whichsam a byte[]

For performance and syntax reasons, I would rather operate on these binary resources as streams instead of byte-arrays. I ve found that, by manually editing the .resx file and changing the name of the class in the <value> element from System.Byte[] to System.IO.MemoryStream, i m able to use the ResourceManager.GetStream() method to successfully access the resources as streams, e.g.

<data name="MyFile" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>..ResourcesMyFile.ext;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>

成为:

<data name="MyFile" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>..ResourcesMyFile.ext;System.IO.MemoryStream, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>

这种做法的唯一缺点是,视觉演播室总是在<条码>下增加新的档案资源。 是否有办法让我打上<代码>MemoryStream的类型?

最佳回答

You can create an extension method on ResourceManager.

public static class ResourceExtensions
{
    public static MemoryStream GetMemoryStream(this ResourceManager resourceManager, String name) {
        object resource = resourceManager.GetObject(name);

        if (resource is byte[]) {
            return new MemoryStream((byte[])resource);
        }
        else {
            throw new System.InvalidCastException("The specified resource is not a binary resource.");
        }
    }
}

呼吁

ResourceManager resourceManager = Properties.Resources.ResourceManager;
MemoryStream stream = resourceManager.GetMemoryStream("binaryResource");

Though, this seems just as well.

MemoryStream stream = new MemoryStream(Properties.Resources.SomeBinaryResource);

我不敢肯定,如果我会修改资源档案,因为资源档案很容易改变,我相信,在视觉演播室会过度变化的情况下,会出现一些情况。

The problem with this, as per your concern, is that this creates a copy of the data into memory, creating a memory footprint. For lightweight resources that will be short lived, its a non-issue, but it can be a large concern.

答案很短: 不能通过避免记忆足迹。 问题是<代码>ResourceManager.GetObject(String)ResourceManager.GetStream(String)都产生了数据副本。 即便是<代码>GetStream(String) 返回<编码> 未管理的MemoryStream,它实际上还在内部打电话到,并且仍在制作一份复印件。 如果你去掉你的申请,记述记忆,你将看到记忆仍然被分配。

我尝试了多种途径,通过在<代码>unsafe的环境下使用点器,以及思考和任何工作。 <代码>Resourceager 并非这种灵活或优化。

然而,我找到了解决办法,但需要你使用<代码>。 表格 这丝毫没有改变,除非你确定你的资源档案的建立行动为<代码>。 http://www.un.org/Depts/DGACM/index_chinese.htm 通过这样做,你可以进行思考,创建<代码>。 未管理的MemoryStream没有生成数据副本。

private UnmanagedMemoryStream GetUnmanagedMemoryStream(String embeddedResourceName) {
    Assembly assembly = Assembly.GetExecutingAssembly();

    string[] resourceNames = assembly.GetManifestResourceNames();
    string resourceName = resourceNames.SingleOrDefault(resource => resource.EndsWith(embeddedResourceName, StringComparison.InvariantCultureIgnoreCase));

    if (resourceName != null) {
        return (UnmanagedMemoryStream)assembly.GetManifestResourceStream(resourceName);
    }
    else {
        throw new System.ArgumentException("The specified embedded resource could not be found.", "embeddedResourceName");
    }
}

我没有对此进行广泛测试,但我确实工作。 我的测试数据是一份小的17个超大型文件。 对我的测试申请的工作记忆从大约50兆字节开始,在将资源重新投入到上游后不会改变。 在使用<代码>ResourceManager时,将按资源的规模立即增加工作。

或许需要取消对<代码>的呼吁。 EndsWith:核对清单中的适当资源名称,因为资源名称与直接通过<代码>查阅资源名称略有不同。 ResourceManager。

我实际上感到失望的是,我未能利用现有的“”找到解决办法,但这只是一个足够灵活的办法。

这里关于这一主题的深入博客文章。

问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签