Skip to content

GenerateResource has incomplete handling for ResXFileRef #4581

Closed

Description

@ericstj

Copied from https://github.com/dotnet/corefx/issues/39980, filed by @Symbai

Accessing a resource file throws a System.MissingMethodException: Constructor on type 'System.Byte[]' not found.

Please see linked issue for repro.

ResX contains

  <data name="TestFile" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>..\Resources\TestFile;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </data>

GenerateResource is treating this as an Activator resource and adding it via AddActivatorResource.
https://github.com/microsoft/msbuild/blob/d31fdbf016616835b237d2846f3f1535560bb2d5/src/Tasks/ResourceHandling/FileStreamResource.cs#L44

Here's the relevant desktop code: https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/Resources/ResXFileRef.cs,313

if(toCreate.Equals(typeof(byte[]))) {
    created = temp;
} else {
    MemoryStream memStream = new MemoryStream(temp);
    if(toCreate.Equals(typeof(MemoryStream))) {
        return memStream;
    } else if(toCreate.Equals(typeof(System.Drawing.Bitmap)) && fileName.EndsWith(".ico")) {
        // we special case the .ico bitmaps because GDI+ destroy the alpha channel component and
        // we don't want that to happen
        Icon ico = new Icon(memStream);
        created = ico.ToBitmap();
    } else {
        created = Activator.CreateInstance(toCreate, BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, null, new Object[] {memStream}, null);
    }
}

It looks like we missed a special case for byte[]. There is also a special case here for MemoryStream that is likely also relevant.

Activity

  1. ericstj commented on Aug 5, 2019

    @ericstj
    MemberAuthor
  2. added this to the MSBuild 16.3 milestone on Aug 5, 2019
  3. self-assigned this
    on Aug 5, 2019
  4. added
    Area: Task: GenerateResource
    on Aug 5, 2019
  5. rainersigwald commented on Aug 5, 2019

    @rainersigwald
    Member

    Investigate MemoryStream/UnmanagedMemoryStream magic (definitely some in StronglyTypedResourceBuilder).

  6. rainersigwald commented on Aug 12, 2019

    @rainersigwald
    Member

    @ericstj do you know if things will work if I instantiate a MemoryStream in .NET Core and pass that as a live object to AddResource, and then try to access it at runtime in .NET Framework? Or will the type names/assemblies mismatch and cause problems?

    (Likewise the other possible combinations of building on {netcoreapp,net472} and targeting {netcoreapp,netstandard,netXX})

    #4607 has a fix for byte[], and it'd be easy enough to wrap it with a MemoryStream but I don't understand the implications.

  7. ericstj commented on Aug 13, 2019

    @ericstj
    MemberAuthor

    It should work if you call one the stream overloads. For these type name isn't stored, but its treated as a primitive type and will provide an UMS at runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

.NET CoreArea: Task: GenerateResourcetriaged

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions