summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Reflection/Metadata/AssemblyExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/mscorlib/src/System/Reflection/Metadata/AssemblyExtensions.cs')
-rw-r--r--src/mscorlib/src/System/Reflection/Metadata/AssemblyExtensions.cs47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/mscorlib/src/System/Reflection/Metadata/AssemblyExtensions.cs b/src/mscorlib/src/System/Reflection/Metadata/AssemblyExtensions.cs
new file mode 100644
index 0000000000..dd5d69b1fb
--- /dev/null
+++ b/src/mscorlib/src/System/Reflection/Metadata/AssemblyExtensions.cs
@@ -0,0 +1,47 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Security;
+
+namespace System.Reflection.Metadata
+{
+ public static class AssemblyExtensions
+ {
+ [DllImport(JitHelpers.QCall)]
+ [SecurityCritical] // unsafe method
+ [SuppressUnmanagedCodeSecurity]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ private unsafe static extern bool InternalTryGetRawMetadata(RuntimeAssembly assembly, ref byte* blob, ref int length);
+
+ // Retrieves the metadata section of the assembly, for use with System.Reflection.Metadata.MetadataReader.
+ // - Returns false upon failure. Metadata might not be available for some assemblies, such as AssemblyBuilder, .NET
+ // native images, etc.
+ // - Callers should not write to the metadata blob
+ // - The metadata blob pointer will remain valid as long as the AssemblyLoadContext with which the assembly is
+ // associated, is alive. The caller is responsible for keeping the assembly object alive while accessing the
+ // metadata blob.
+ [CLSCompliant(false)] // out byte* blob
+ [SecurityCritical] // unsafe method
+ public unsafe static bool TryGetRawMetadata(this Assembly assembly, out byte* blob, out int length)
+ {
+ if (assembly == null)
+ {
+ throw new ArgumentNullException("assembly");
+ }
+
+ blob = null;
+ length = 0;
+
+ var runtimeAssembly = assembly as RuntimeAssembly;
+ if (runtimeAssembly == null)
+ {
+ return false;
+ }
+
+ return InternalTryGetRawMetadata(runtimeAssembly, ref blob, ref length);
+ }
+ }
+}