Skip to content

Type.GetType: improve code analysis when throwOnError is provided #116330

Closed as not planned
@weitzhandler

Description

@weitzhandler

Description

Please improve compiler the methods in System.Type where a throwOnError is applicable by specifying code-analysis.
We're not expecting null if throwOnError is true.

Proposal

namespace System

public abstract partial class Type : MemberInfo, IReflect
{
    [SupportedOSPlatform("windows")]
    public static Type? GetTypeFromCLSID(Guid clsid, bool throwOnError) =>
        GetTypeFromCLSID(clsid, null, throwOnError: throwOnError);

    [SupportedOSPlatform("windows")]
    public static Type? GetTypeFromCLSID(Guid clsid, string? server, bool throwOnError) => 
        Marshal.GetTypeFromCLSID(clsid, server, throwOnError);

    [SupportedOSPlatform("windows")]
    public static Type? GetTypeFromProgID(string progID, bool throwOnError) => 
        GetTypeFromProgID(progID, null, throwOnError: throwOnError);

    [SupportedOSPlatform("windows")]
    public static Type? GetTypeFromProgID(string progID, string? server, bool throwOnError) =>
        Marshal.GetTypeFromProgID(progID, server, throwOnError);
}

The above should be changed to:

public abstract partial class Type : MemberInfo, IReflect
{
    [SupportedOSPlatform("windows")]
    public static Type? GetTypeFromCLSID(Guid clsid, [NotNullWhen(true)] bool throwOnError) =>
        GetTypeFromCLSID(clsid, null, throwOnError: throwOnError);
    [SupportedOSPlatform("windows")]
    public static Type? GetTypeFromCLSID(Guid clsid, string? server, [NotNullWhen(true)] bool throwOnError) =>
        Marshal.GetTypeFromCLSID(clsid, server, throwOnError);

    [SupportedOSPlatform("windows")]
    public static Type? GetTypeFromProgID(string progID, [NotNullWhen(true)] bool throwOnError) =>
        GetTypeFromProgID(progID, null, throwOnError: throwOnError);
    [SupportedOSPlatform("windows")]
    public static Type? GetTypeFromProgID(string progID, string? server, [NotNullWhen(true)] bool throwOnError) =>
        Marshal.GetTypeFromProgID(progID, server, throwOnError);
}

Feel free to assign me if approved.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions