オブジェクトをトリミングするための一般的な一般的な方法が1つありました
public static T GetTrimmedObject<T>(T model, bool isWantToEncodeStrings = false) where T : class
{
try
{
List<PropertyInfo> propertiesInfoCollection = model.GetType().GetProperties().ToList();
foreach (PropertyInfo item in propertiesInfoCollection)
{
Type type = item.PropertyType;
if (type == typeof(String))
{
dynamic value = item.GetValue(model, null);
if (!String.IsNullOrWhiteSpace(value))
{
item.SetValue(model, isWantToEncodeStrings ? HttpUtility.HtmlEncode(value.Trim()) : value.Trim(), null);
}
}
}
return model;
}
catch (Exception ex)
{
HandleAndLogException(ex, typeof(GlobalUtil));
return model;
}
}
しかし、プロパティがクラスのタイプの場合、再帰呼び出しが必要なため、ネストされたクラス プロパティも消去されます。だから私の質問は、同じことを確認する方法ですか?