Skip to content

Generate explicit cast operators #206

@Herdo

Description

@Herdo

Coming from the strong type generator I wrote a few years ago using T4, I'm missing the functionality of using explicit casts.

The existing methods, e.g. InternalUserId.From(42) do their job, but migrating would be painful, as the current code base accross a hundred solutions all use explicit casts for conversion into strong types: var userId = (InternalUserId)42;.

The linked T4 template generates two explicit operators (types in code just as example) that allow easy yet explicit conversion only:

/// <summary>
/// Converts the weak type into a <see cref="InternalUserId"/> instance.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>A new instance of the strong type.</returns>
[ExcludeFromCodeCoverage]
public static explicit operator InternalUserId(System.Int32 value)
{
    return new InternalUserId(value);
}

/// <summary>
/// Converts the strong type into a <see cref="System.Int32"/> value.
/// </summary>
/// <param name="value">The instance to convert.</param>
/// <returns>The converted value.</returns>
[ExcludeFromCodeCoverage]
public static explicit operator System.Int32(InternalUserId value)
{
    return value.V;
}

Without much overhead, this would allow for even easier user of value objects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions