修剪所有字符串属性 [英] Trim all string properties

查看:124
本文介绍了修剪所有字符串属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的对象来修剪一些字符串属性,但我不想去的所有对象和属性,并在集propertie做修剪方法(有大量的对象,300家和很多。字符串属性)



一个建议:我所有的对象都被称为CoreTransaction一个超类,所以我可以用它(具有某种反射)做这件事伊斯利



这可能吗?



感谢您。


解决方案

  VAR stringProperties = obj.GetType()的GetProperties()
。凡(p =方式> p.PropertyType == typeof运算(字符串));

的foreach(在stringProperties VAR stringProperty)
{
串CurrentValue的=(字符串)stringProperty.GetValue(OBJ,NULL);
stringProperty.SetValue(OBJ,currentValue.Trim(),NULL);
}


I need to Trim some string properties in my objects, but I don't want to go to all objects and properties and in the set propertie do the Trim method (there is a lot of objects, 300+ and a lot of string properties).

One tip: all my objects have a super class called CoreTransaction, so I can use it (with some kind of reflection) to do this thing more easly.

Is that possible?

Thank you.

解决方案

var stringProperties = obj.GetType().GetProperties()
                          .Where(p => p.PropertyType == typeof (string));

foreach (var stringProperty in stringProperties)
{
    string currentValue = (string) stringProperty.GetValue(obj, null);
    stringProperty.SetValue(obj, currentValue.Trim(), null) ;
}

这篇关于修剪所有字符串属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆