0

オブジェクトをトリミングするための一般的な一般的な方法が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;
        }
    }

しかし、プロパティがクラスのタイプの場合、再帰呼び出しが必要なため、ネストされたクラス プロパティも消去されます。だから私の質問は、同じことを確認する方法ですか?

4

1 に答える 1

0

Type.IsPrimitive がクラス型であるかどうかを確認し、指定された型で関数を再帰的に呼び出すことができます。

于 2013-09-12T11:20:14.950 に答える