Bagaimana cara memanggil implementasi anggota antarmuka saya sendiri?

pemrograman


Saya mencoba membuat kelas (Pengukuran) yang seharusnya meniru kelas numerik C# internal (ke dalam, dobel, mengambang, dll) sedekat mungkin. Untuk tujuan ini saya telah mendefinisikannya Pengukuran kelas untuk memiliki implementasi sendiri dari metode yang digunakan oleh kelas C#.

Namun, saya mengalami kesulitan mengakses implementasi baru. Contoh kode di bawah ini menunjukkan sintaks yang saya gunakan.

Kelas saya mencakup yang berikut ini untuk mendefinisikan Coba KonversiDari Diperiksa() metode:

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>
        {
            ...
        }
...
    }

Dan saya mencoba memanggil metode itu sebagai berikut:

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)}]");
    }

Tapi ini selalu mengeksekusi internal C# Sistem.Numerik.INumberBase penerapan. Bagaimana cara saya menerima telepon saya Coba KonversiDari Diperiksa() untuk memanggil yang telah saya buat?

Apa yang saya coba:

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

Hasil di CS0704 Tidak dapat melakukan pencarian anggota non-virtual di ‘T’ karena ini adalah parameter tipe

Solusi 1

Inilah masalah Anda:

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

Ini memanggil TryConvertFromChecked dalam readonly struct Double. Inilah caranya:

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

dan itu 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;
    }
}

Konversi tidak terbuka untuk penyesuaian sesuai keinginan Anda. Anda akan lebih baik melihat menggunakan Kelas TypeConverter (Sistem.ComponentModel) | Microsoft Belajar[^]

コメント

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