遍历对象自己的字符串和字符串修剪每个
我有多个大对象,每个对象都有大约 60 个字符串。我必须修剪所有这些字符串,并且我想这样做而不必去 this.mystring = this.mystring.Trim()。相反,我正在寻找一种方法来自动让每个对象发现自己的字符串,然后执行操作。
我对反射了解一点,但还不够,但我认为这可能吗?
另外,我不确定这是否重要,但某些字符串属性是只读的(只有吸气剂),因此必须跳过这些属性。
帮助?
I have multiple large objects which each have about 60 strings. I have to trim all those strings, and I'd like to do so without having to go this.mystring = this.mystring.Trim(). Instead, I'm looking for a way to automatically have each object discover its own strings and then perform the operation.
I know a little bit about reflection, but not enough, but I think this is possible?
Also, I'm not sure if this matters, but some string properties are read-only (only have a getter), so those properties would have to be skipped.
Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,获取所有属性并找出哪些是字符串且可写是很容易的。 LINQ 使其变得更加容易。
您可能只想在修剪确实产生影响的情况下设置属性,以避免复杂属性的冗余计算 - 或者这对您来说可能不是问题。
如果需要,有多种方法可以提高性能 - 例如:
Delegate.CreateDelegate
为 getter 和 setter 构建委托除非性能实际上是一个问题,否则我不会采取任何这些步骤。
Well, it's easy enough to get all the properties, and find out which ones are strings and writable. LINQ makes it even easier.
You may want to only set the property if trimming actually makes a difference, to avoid redundant computations for complex properties - or it may not be an issue for you.
There are various ways of improving the performance if necessary - things like:
Delegate.CreateDelegate
to build delegates for the getters and settersI wouldn't take any of those steps unless performance is actually a problem though.
像这样的东西:
Something like:
无需在 props-loop 中进行 IEnumerable 检查,如果实际实例是 IEnumerable,则忽略 props。修复
IEnumerable
部分:Not necessary to make
IEnumerable
check in the props-loop and if actual instance is aIEnumerable
, props are ignored. Fix forIEnumerable
part:因此,为了稍微扩展一下,我有一个带有列表列表的复杂对象,我想遍历它并修剪所有子字符串对象。我正在发布我在 @Jon 在他的回答中所做的事情。我很好奇是否有更好的方法,或者我是否错过了一些明显的事情。
我拥有的对象比这更复杂,但它应该说明我正在尝试的内容。
想法?
So to expand on this a little, I have a complex object with Lists of Lists and I wanted to traverse that and trim all of the child string objects as well. I'm posting what I did as of what I built on from @Jon did in his answer. I'm curious if there was a better way to do it or if I missed something obvious.
The objects I have are more complex than this but it should illustrate what I was trying.
Thoughts?