- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.2k
Implement RuntimeHelpers.SizeOf #100618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement RuntimeHelpers.SizeOf #100618
Changes from 15 commits
3362c4a
              47c6a24
              16ea830
              cb7ec92
              47d3b54
              c086418
              84b4c0f
              49e6521
              f93baee
              6898911
              3423a52
              1c473f6
              21fde4e
              51a93b2
              ec81d77
              ed05d1a
              7908cf6
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -211,5 +211,29 @@ private static extern unsafe IntPtr GetSpanDataFrom( | |
|  | ||
| [MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
| private static extern bool SufficientExecutionStack(); | ||
|  | ||
| [MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
| private static extern int SizeOf(QCallTypeHandle handle); | ||
|  | ||
| /// <summary> | ||
| /// Get the size of an object of the given type. | ||
| /// </summary> | ||
| /// <param name="type">The type to get the size of.</param> | ||
| /// <returns>The size of instances of the type.</returns> | ||
| /// <exception cref="ArgumentException">The passed-in type is not a valid type to get the size of.</exception> | ||
| /// <remarks> | ||
| /// This API has the same behavior as if you were to use the IL sizeof instruction with the passed in type as the operand. | ||
| /// </remarks> | ||
| public static int SizeOf(RuntimeTypeHandle type) | ||
| { | ||
| if (type.Value == IntPtr.Zero) | ||
| ThrowHelper.ThrowArgumentNullException(ExceptionArgument.type); | ||
|  | ||
| Type typeObj = Type.GetTypeFromHandle(type)!; | ||
| if (typeObj.ContainsGenericParameters || typeObj.IsGenericParameter || typeObj == typeof(void)) | ||
| throw new ArgumentException(SR.Arg_TypeNotSupported); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a limitation of mono runtime (which doesn't exist in coreclr)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this is the same limitation in CoreCLR and NativeAOT (though the implementations are slightly different due to how the different type systems are implemented). | ||
|  | ||
| return SizeOf(new QCallTypeHandle(ref type)); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: It does not have the same behavior for error cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe even link to https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.unsafe.sizeof that has more details.