ironfede / openmcdf

Microsoft Compound File .net component - pure C# - netstandard 2.0
Mozilla Public License 2.0
295 stars 72 forks source link

Is there a need to use Array.CreateInstance(typeof(byte)) to create b… #126

Open Numpsy opened 3 months ago

Numpsy commented 3 months ago

…yte arrays?

I tried building an application that uses OpenMcdf as NativeAOT (.NET 8.0) and got this warning from the compiler:

16:07:28:585    3>OpenMcdf.CompoundFile.Commit(Boolean): Using member 'System.Array.CreateInstance(Type,Int32)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available.

I guess the code for byte arrays should always exist given they're used elsewhere, but - I'm not sure if there is a need to use Array.CreateInstance rather than just new[] anyway?

ironfede commented 2 months ago

Hi @Numpsy , honestly speaking I can't actually remember the original thinking that pushed me to use this syntax to create arrays... Have you checked Microsoft documentation for some subtle difference? I think that it should work. Stack Overflow address exactly this point here

Numpsy commented 2 months ago

I'm not aware of any differences in the date the produce (e.g. whether they differ in how the data is initialized etc). I can't say offhand if there might have been any difference in some older .NET Version.

filipnavara commented 19 hours ago

@ironfede Can you please merge it? I checked with AOT analyzers turned on that this is the only code flagged as not compatible.

Here's a diff to run the build as net8.0 with the analyzers turned on:

--- a/sources/OpenMcdf/OpenMcdf.csproj
+++ b/sources/OpenMcdf/OpenMcdf.csproj
@@ -1,6 +1,6 @@
 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
-    <TargetFrameworks>netstandard2.0;net40</TargetFrameworks>
+    <TargetFrameworks>netstandard2.0;net40;net80</TargetFrameworks>^M
     <Configurations>Debug;Release</Configurations>
     <SignAssembly>true</SignAssembly>
     <!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
@@ -18,6 +18,7 @@
     <GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
     <GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
     <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
+    <IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>^M
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>