【解決方法】インターフェイス メンバーの独自の実装を呼び出すにはどうすればよいですか?


クラスを作成しようとしています(測定) 内部 C# 数値クラス (整数ダブル浮く、など)できるだけ正確に。 この目的のために、私は次のように定義しました。 測定 クラスには、C# クラスで使用されるメソッドの独自の実装が含まれます。

ただし、新しい実装にアクセスするのに問題があります。 以下のコード例は、私が使用している構文を示しています。

私のクラスには、 TryConvertFromChecked() 方法:

C#
public partial class Measurement<T> : ..., INumberBase<Measurement<T>>,...
        where T : ...,INumberBase<T>, ...
    {
...
        /// <inheritdoc cref="INumberBase{TSelf}.TryConvertFromChecked{TOther}(TOther, out TSelf)" />
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static bool INumberBase<Measurement<T>>.TryConvertFromChecked<TOther>(TOther value, [MaybeNullWhen(false)] out Measurement<T> result)
        {
            return TryConvertFrom(value, out result, ConversionOption.Checked);
        }

        private static bool TryConvertFrom<TOther>([NotNull] TOther value, [MaybeNullWhen(false)] out Measurement<T> result, ConversionOption option)
            where TOther : INumberBase<TOther>
        {
            ...
        }
...
    }

そして、そのメソッドを次のように呼び出そうとしています。

C#
if (!T.TryConvertFromChecked(toUnit.CompoundFactor, out var toCompoundFactor))
    {
        throw new ApplicationException($"An internal error occurred while converting the CompoundFactor for [{unitSymbol}] from type [{toUnit.CompoundFactor}] to type [{typeof(T)}]");
    }

ただし、これは常に C# の内部プログラムを実行しています。 System.Numerics.INumberBase 実装。 電話をかけるにはどうすればよいですか TryConvertFromChecked() 私が作成したものを呼び出すには?

私が試したこと:

C#
if (!T.Measurement<T>.TryConvertFromChecked(toUnit.CompoundFactor, out var toCompoundFactor))

の結果 CS0704 ‘T’ は型パラメータであるため、非仮想メンバーの検索を実行できません

解決策 1

あなたの問題は次のとおりです。

C#
T.TryConvertFromChecked(toUnit.CompoundFactor, out var toCompoundFactor)

これは、 TryConvertFromChecked の中に readonly struct Double。 その方法は次のとおりです。

C#
static bool INumberBase<double>.TryConvertFromChecked<TOther>(TOther value, out double result)
{
    return TryConvertFrom<TOther>(value, out result);
}

そしてその TryConvertFrom:

C#
private static bool TryConvertFrom<TOther>(TOther value, out double result)
    where TOther : INumberBase<TOther>
{
    // In order to reduce overall code duplication and improve the inlinabilty of these
    // methods for the corelib types we have `ConvertFrom` handle the same sign and
    // `ConvertTo` handle the opposite sign. However, since there is an uneven split
    // between signed and unsigned types, the one that handles unsigned will also
    // handle `Decimal`.
    //
    // That is, `ConvertFrom` for `double` will handle the other signed types and
    // `ConvertTo` will handle the unsigned types

    if (typeof(TOther) == typeof(Half))
    {
        Half actualValue = (Half)(object)value;
        result = (double)actualValue;
        return true;
    }
    else if (typeof(TOther) == typeof(short))
    {
        short actualValue = (short)(object)value;
        result = actualValue;
        return true;
    }
    else if (typeof(TOther) == typeof(int))
    {
        int actualValue = (int)(object)value;
        result = actualValue;
        return true;
    }
    else if (typeof(TOther) == typeof(long))
    {
        long actualValue = (long)(object)value;
        result = actualValue;
        return true;
    }
    else if (typeof(TOther) == typeof(Int128))
    {
        Int128 actualValue = (Int128)(object)value;
        result = (double)actualValue;
        return true;
    }
    else if (typeof(TOther) == typeof(nint))
    {
        nint actualValue = (nint)(object)value;
        result = actualValue;
        return true;
    }
    else if (typeof(TOther) == typeof(sbyte))
    {
        sbyte actualValue = (sbyte)(object)value;
        result = actualValue;
        return true;
    }
    else if (typeof(TOther) == typeof(float))
    {
        float actualValue = (float)(object)value;
        result = actualValue;
        return true;
    }
    else
    {
        result = default;
        return false;
    }
}

変換は、希望する方法でカスタマイズすることができません。 を使用して検討したほうがよいでしょう TypeConverter クラス (System.ComponentModel) | Microsoft Learn[^]

ただし、それでもこのルートを進みたい場合は、以下を参照してください。 .NET ソース ブラウザ – BigInteger[^] Microsoft がそれにどのようにアプローチしているかを確認してください。

コメント

タイトルとURLをコピーしました