Skip to content

Commit 0d0f787

Browse files
rolfbjarneCopilot
andauthored
[ObjCRuntime] Enable nullability and clean up TypeConverter. (#24733)
This is file 2 of 7 files with nullability disabled in ObjCRuntime. * Enable nullability (#nullable enable). * Add nullability-aware argument validation and element-type checks using ThrowHelper for static analysis. * Improve XML documentation comments: fix formatting/order, grammar, and add relevant see cref references. Contributes towards #17285. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5849bb5 commit 0d0f787

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/ObjCRuntime/TypeConverter.cs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
using System.Reflection;
44
using System.Collections.Generic;
55

6-
// Disable until we get around to enable + fix any issues.
7-
#nullable disable
6+
#nullable enable
87

98
namespace ObjCRuntime {
109

11-
/// <summary>Converts Obj-C type encodings to managed types.</summary>
12-
/// <remarks>
13-
/// <para>This class provides a way of converting Objective-C encoded type strings to .NET and viceversa. The full details about type encodings are available <format type="html"><a href="https://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/Type-encoding.html">here</a></format>.
14-
/// </para>
15-
/// </remarks>
10+
/// <summary>Converts Objective-C type encodings to managed types and vice versa.</summary>
11+
/// <remarks>
12+
/// <para>This class provides a way to convert Objective-C encoded type strings to .NET types and vice versa.</para>
13+
/// <para>The full details about type encodings are available <see href="https://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/Type-encoding.html">here</see>.</para>
14+
/// </remarks>
1615
public static class TypeConverter {
1716
#if !COREBUILD
1817
/*
@@ -21,15 +20,19 @@ public static class TypeConverter {
2120
*
2221
* http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/Type-encoding.html
2322
*/
24-
/// <param name="type">Type description.</param>
25-
/// <summary>Converts the specified Objective-C description into the .NET type.</summary>
26-
/// <returns>The .NET type.</returns>
27-
/// <remarks>
28-
/// <para>For example: TypeConverter.ToManaged ("@") returns typeof (IntPtr).</para>
29-
/// </remarks>
23+
/// <summary>Converts the specified Objective-C type encoding into the corresponding managed <see cref="Type"/>.</summary>
24+
/// <param name="type">The Objective-C type encoding.</param>
25+
/// <returns>The managed <see cref="Type"/>.</returns>
26+
/// <remarks>
27+
/// <para>For example, <c>TypeConverter.ToManaged ("@")</c> returns <c>typeof (IntPtr)</c>.</para>
28+
/// </remarks>
3029
[BindingImpl (BindingImplOptions.Optimizable)] // To inline the Runtime.DynamicRegistrationSupported code if possible.
3130
public static Type ToManaged (string type)
3231
{
32+
type = ThrowHelper.ThrowIfNull (type);
33+
if (type.Length == 0)
34+
ThrowHelper.ThrowArgumentException (nameof (type), "The type encoding must not be empty.");
35+
3336
if (!Runtime.DynamicRegistrationSupported) // The call to Runtime.GetAssemblies further below requires the dynamic registrar.
3437
throw ErrorHelper.CreateError (8026, "TypeConverter.ToManaged is not supported when the dynamic registrar has been linked away.");
3538

@@ -107,18 +110,23 @@ public static Type ToManaged (string type)
107110
*
108111
* http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/Type-encoding.html
109112
*/
110-
/// <param name="type">A .NET type.</param>
111-
/// <summary>Converts a .NET type into the Objective-C type code.</summary>
112-
/// <returns />
113-
/// <remarks>
114-
/// <para>For example: TypeConverter.ToNative (int.GetType ()) will return "i".</para>
115-
/// </remarks>
113+
/// <summary>Converts a managed <see cref="Type"/> into the corresponding Objective-C type encoding.</summary>
114+
/// <param name="type">The managed <see cref="Type"/>.</param>
115+
/// <returns>The Objective-C type encoding.</returns>
116+
/// <remarks>
117+
/// <para>For example, <c>TypeConverter.ToNative (typeof (int))</c> returns <c>"i"</c>.</para>
118+
/// </remarks>
116119
public static string ToNative (Type type)
117120
{
121+
type = ThrowHelper.ThrowIfNull (type);
122+
118123
if (type.IsGenericParameter)
119124
throw new ArgumentException ("Unable to convert generic types");
120125

121-
if (type.IsByRef) return "^" + ToNative (type.GetElementType ());
126+
if (type.IsByRef) {
127+
var elementType = ThrowHelper.ThrowIfNull (type.GetElementType ());
128+
return "^" + ToNative (elementType);
129+
}
122130
if (type == typeof (IntPtr)) return "^v";
123131
if (type == typeof (byte)) return "C";
124132
if (type == typeof (sbyte)) return "c";

0 commit comments

Comments
 (0)