Today, meow encountered a problem.
Some string fields in the database I designed are fixed-length strings. The entity values are extracted from the string with extra spaces.
So I want to remove the extra spaces.
I wrote a method to remove extra spaces in the string attribute of the entity and used reflection.
This removes unnecessary spaces.
Code:
- /// <Summary>
- /// Remove unnecessary spaces in the string attribute of the class
- /// </Summary>
- /// <Param name = "OBJ"> </param>
- /// <Returns> </returns>
- Public static object trimstr (Object OBJ)
- {
- Type T = obj. GetType ();
- // Attribute array
- Propertyinfo [] properties = T. getproperties ();
- // Traverses the attribute array
- Foreach (propertyinfo P in properties)
- {
- If (P. propertytype. Name = "string") // string attribute
- {
- // Obtain the value
- String STR = (string) p. getvalue (OBJ, null );
- // Assign a value again
- P. setvalue (OBJ, str. Trim (), null );
- }
- }
- Return OBJ;
- }