'null'プロパティの値を 'null以外'の値に設定する方法を探しています。これらのプロパティはオブジェクトに関連付けられ、複数のオブジェクトのリストが存在します。オブジェクトのプロパティからヌルを削除する
私が抱えている問題は、 'null'値を 'null以外'の値に変換して、各プロパティのタイプが異なる場合です。
私が今まで持っていたのは、ヌルプロパティを識別してnull以外に設定しようとして、いくつかのネストされたループと条件です。
//loop through each object
for (int i = 0; i < objectList.Count; i++)
{
//loop through each object and each field within that object
foreach (var property in objectList[i].GetType().GetProperties())
{
var current_field_val = property.GetValue(objectList[i], null);
//null validation
if (current_field_val == null)
{
PropertyInfo current_field_data_type = objectList[i].GetType().GetProperty(property.Name);
if (current_field_data_type is String)
{
objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], "");
}
else if (current_field_data_type is int)
{
objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], 0);
}
else if (current_field_data_type is double)
{
objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], 1);
}
else if (current_field_data_type is object)
{
objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], "");
}
}
}
}
私の貧弱なインデントは許されません。VSは前後にコピーするとうまくいきませんでした。
クリーンアップのおかげで@Damian! – user3801447
あなたの質問は何ですか?期待どおりに動かないのは何ですか? – InBetween
これは非常に悪い考えです。 intとdoublesは、nullとすることもできます。基本的には、デザインの初期段階では間違っていました。これは厄介なハックです。 –