修剪所有字符串属性
我需要修剪对象中的一些字符串属性,但我不想转到所有对象和属性,并且在设置属性中执行修剪方法(有很多对象,300+和很多字符串属性) 。
一个提示:我的所有对象都有一个名为 CoreTransaction 的超类,因此我可以使用它(通过某种反射)更轻松地完成此操作。
这可能吗?
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 properties 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 easily.
Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
感谢 Bala R 解决了 OP 的问题。我将您的解决方案转换为扩展方法,并修复了空值引发错误的问题。
Thank you to Bala R for your solution to the OP's problem. I converted your solution to an extension method and fixed a problem where null values were throwing errors.
我修复了兰迪的答案,以容纳子可为空对象并处理 IEnumerable 集合(循环遍历对象列表并修剪字符串属性)。我对他的答案进行了编辑,但由于与主题无关而被拒绝,但那是一堆垃圾。希望这对某人有帮助,因为兰迪的答案并不适用于我拥有的每种对象类型。现在确实如此。
I fixed landi's answer to accommodate child nullable objects and handle IEnumerable collections (loop through a List of object and trim string properties). I made an edit to his answer which was rejected for not being on topic, but that's a load of garbage. Hopefully this helps someone, as landi's answer didn't work on every object type I had. Now it does.
我编写了一个扩展方法,它还处理引用类上的子类和字符串(如parent.Child.Name)
I have written a Extension Method which also takes care of subclasses and strings on referenced classes (like parent.Child.Name)
我不确定是否要改变访问者的行为。这听起来一点也不容易。将修剪添加到您的基类怎么样?
(另外,请注意检查您的字段是否可以读取和写入。
)
编辑:然后,您可以将类似的内容添加到您的基类中,并一次性修剪所有它们。
WeakReference 类将允许您轻松跟踪实例,而不会妨碍垃圾收集器:
I'm not sure about changing the behaviour of your accessors. That doesn't sound easy at all. How about adding the trimming to your base class?
(Also, note the check that your fields can be both read and written to.)
EDIT: You could then add something like this to your base class, and trim all of them in one go.
The WeakReference class will allow you to easily keep track of your instances without getting in the way of the garbage collector:
您可以使用反射来做这样的事情:
You could use reflection to do something like this:
感谢兰迪的解决方案。我更改了他的方法以添加对带有索引参数的类的支持,并在继续之前检查 obj 是否为空。
Thank landi for his solution. I altered his method to add support for Classes with Index Parameters and also to check if the obj is null before continuing.
扩展了自己的解决方案并添加了一项检查,以确保可以写入属性。由于 Uri 属性而出现一些“未找到属性设置方法”错误。
Extended Own's solution and added a check that it's possible to write to property. Had some "Property set method not found" errors due to a Uri property.
对于那些使用 VB.NET 的人,我转换了 thrawnis 的答案并添加了一个条件以仅返回那些非只读属性。否则,如果您的类具有只读属性,当您尝试为这些属性设置值时,您将收到运行时错误。
For those who use VB.NET, I converted thrawnis's answer and added a condition to only return those properties that are not ReadOnly. Otherwise if your class has readonly properties you will get runtime error when you try to SetValue for those properties.
你可以尝试一下:
像这样使用它:
You can try it:
Use it like this :
我采用了 OwN 的答案,但做了这些更改:
if
ObjectExtensions.cs
ObjectExtensionsTests.cs
I took OwN's answer but made these changes:
if
var
everywhere, renaming a few variablesObjectExtensions.cs
ObjectExtensionsTests.cs
感谢@Teter28 提出的代码生成想法。它比反射解决方案更有效。
提供的代码示例不起作用。这是准备使用的示例。
Thanks @Teter28 for the idea with code generation. It's much more effective than solutions with reflection.
The provided code example does not work. Here's ready for use example.
这是支持嵌套集合和字符串的解决方案:
Here is a solution supporting nested collections and strings: