ここで達成しようとしているのは、ボックス化されたプリミティブ型の単純な値の比較です。
((object)12).Equals((object)12); // Type match will result in a value comparison,
((object)12).Equals((object)12d); // but a type mismatch will not. (false)
object.Equals((object)12,(object)12d); // Same here. (false)
「理由」はわかります。「方法」がわかりません。
型は実行時まで不明であり、データソースのプリミティブ型である可能性があります。これには、文字列、日付時刻、ブール値などが含まれます。両方の型を処理する拡張メソッドを作成し、'==' 比較を行う前にキャストするという醜いルートをたどりました: (完全を期すために、すべてのプリミティブ型を含めました。 、および私が興味を持っていたもの)
public static bool ValueEquals(this object thisObj, object compare)
{
if (thisObj is int)
{
int obj = (int)thisObj;
if (compare is int)
return (obj == (int)compare);
if (compare is uint)
return (obj == (uint)compare);
if (compare is decimal)
return (obj == (decimal)compare);
if (compare is float)
return (obj == (float)compare);
<... and so on for each primitive type ...>
}
if (thisObj is uint)
{
uint obj = (uint)thisObj;
if (compare is int)
return (obj == (int)compare);
if (compare is uint)
return (obj == (uint)compare);
<... Again for each primitive type ...>
}
if (thisObj is decimal)
{
decimal obj = (decimal)thisObj;
if (compare is int)
return (obj == (int)compare);
<... Etc, etc ...>
結果として得られたメソッドは 300 行以上の長さであることが判明しましたが、これは問題ありませんでしたが (それでも恐ろしく)、「==」以上のことを行う必要があります。>、<、<=、>=、!= が必要です。
ボックス化された値の型の比較に使用できるリフレクションに何かありますか?
まったく何か?