Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:24:58+00:00 2026-05-31T08:24:58+00:00

the following function needs to look inside the input object if there is a

  • 0

the following function needs to look inside the input object if there is a property in it that returns a custom object it needs to do the trimming of that object as well. The code below works for the input object fine, but wont recursively look into a property that returns a custom object and do the trimming process.

public object TrimObjectValues(object instance)
{
    var props = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
                // Ignore non-string properties
                .Where(prop => prop.PropertyType == typeof(string) | prop.PropertyType == typeof(object))
                // Ignore indexers
                .Where(prop => prop.GetIndexParameters().Length == 0)
                // Must be both readable and writable
                .Where(prop => prop.CanWrite && prop.CanRead);

    foreach (PropertyInfo prop in props)
    {
        if (prop.PropertyType == typeof(string))
        {
            string value = (string)prop.GetValue(instance, null);
            if (value != null)
            {
                value = value.Trim();
                prop.SetValue(instance, value, null);
            }
        }
        else if (prop.PropertyType == typeof(object))
        {
            TrimObjectValues(prop);
        }
    }

    return instance;
}

I need to change this somehow to look for other objects inside the initial object

.Where(prop => prop.PropertyType == typeof(string) | prop.PropertyType == typeof(object))

This code isn’t working reason is for a example is the object I am passing as input has a property that returns a type of “Address” therefore typeof(object) never gets hit.

Here is a tree of data to test against pass the function “o” in this case

        Order o = new Order();

    o.OrderUniqueIdentifier = "TYBDEU83e4e4Ywow";

    o.VendorName = "Kwhatever";
    o.SoldToCustomerID = "Abc98971";
    o.OrderType = OrderType.OnOrBefore;
    o.CustomerPurchaseOrderNumber = "MOOMOO 56384";
    o.EmailAddress = "abc@electric.com";
    o.DeliveryDate = DateTime.Now.AddDays(35);

    Address address1 = new Address();
    //address1.AddressID = "Z0mmn01034";
    address1.AddressID = "E0000bbb6                         ";
    address1.OrganizationName = "                                       Nicks Organization ";
    address1.AddressLine1 = "              143 E. WASHINGTON STREET                ";
    address1.City = "          Rock        ";
    address1.State = "MA                       ";
    address1.ZipCode = "                         61114";
    address1.Country = "US                ";

    o.ShipToAddress = address1;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:25:00+00:00Added an answer on May 31, 2026 at 8:25 am

    Your tests with typeof(object) will all fail.

    Try like this:

    static void TrimObjectValues(object instance)
    {
        // if the instance is null we have nothing to do here
        if (instance == null)
        {
            return;
        }
    
        var props = instance
            .GetType()
            .GetProperties(BindingFlags.Instance | BindingFlags.Public)
            // Ignore indexers
            .Where(prop => prop.GetIndexParameters().Length == 0)
            // Must be both readable and writable
            .Where(prop => prop.CanWrite && prop.CanRead);
    
        foreach (PropertyInfo prop in props)
        {
            if (prop.PropertyType == typeof(string))
            {
                // if we have a string property we trim it
                string value = (string)prop.GetValue(instance, null);
                if (value != null)
                {
                    value = value.Trim();
                    prop.SetValue(instance, value, null);
                }
            }
            else
            {
                // if we don't have a string property we recurse
                TrimObjectValues(prop.GetValue(instance, null));
            }
        }
    }
    

    I have also made the function return no value because you are modifying the argument instance anyway.

    Test case:

    public enum OrderType
    {
        OnOrBefore
    }
    
    public class Order
    {
        public string OrderUniqueIdentifier { get; set; }
        public string VendorName { get; set; }
        public string SoldToCustomerID { get; set; }
        public OrderType OrderType { get; set; }
        public string CustomerPurchaseOrderNumber { get; set; }
        public string EmailAddress { get; set; }
        public DateTime DeliveryDate { get; set; }
        public Address ShipToAddress { get; set; }
    }
    
    public class Address
    {
        public string AddressID { get; set; }
        public string OrganizationName { get; set; }
        public string AddressLine1 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
        public string Country { get; set; }
    }
    
    class Program
    {
        static void Main()
        {
            Order o = new Order();
    
            o.OrderUniqueIdentifier = "TYBDEU83e4e4Ywow";
    
            o.VendorName = "Kwhatever";
            o.SoldToCustomerID = "Abc98971";
            o.OrderType = OrderType.OnOrBefore;
            o.CustomerPurchaseOrderNumber = "MOOMOO 56384";
            o.EmailAddress = "abc@electric.com";
            o.DeliveryDate = DateTime.Now.AddDays(35);
    
            Address address1 = new Address();
            //address1.AddressID = "Z0mmn01034";
            address1.AddressID = "E0000bbb6                         ";
            address1.OrganizationName = "                                       Nicks Organization ";
            address1.AddressLine1 = "              143 E. WASHINGTON STREET                ";
            address1.City = "          Rock        ";
            address1.State = "MA                       ";
            address1.ZipCode = "                         61114";
            address1.Country = "US                ";
    
            o.ShipToAddress = address1;
    
    
            TrimObjectValues(o);
        }
    
        static void TrimObjectValues(object instance)
        {
            if (instance == null)
            {
                return;
            }
    
            var props = instance
                .GetType()
                .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                // Ignore indexers
                .Where(prop => prop.GetIndexParameters().Length == 0)
                // Must be both readable and writable
                .Where(prop => prop.CanWrite && prop.CanRead);
    
            foreach (PropertyInfo prop in props)
            {
                if (prop.PropertyType == typeof(string))
                {
                    string value = (string)prop.GetValue(instance, null);
                    if (value != null)
                    {
                        value = value.Trim();
                        prop.SetValue(instance, value, null);
                    }
                }
                else
                {
                    TrimObjectValues(prop.GetValue(instance, null));
                }
            }
        }
    }
    

    UPDATE 2:

    It seems that you want to handle also lists of objects. You could adapt the method:

    static void TrimObjectValues(object instance)
    {
        if (instance == null)
        {
            return;
        }
    
        var props = instance
            .GetType()
            .GetProperties(BindingFlags.Instance | BindingFlags.Public)
            // Ignore indexers
            .Where(prop => prop.GetIndexParameters().Length == 0)
            // Must be both readable and writable
            .Where(prop => prop.CanWrite && prop.CanRead);
    
        if (instance is IEnumerable)
        {
            foreach (var element in (IEnumerable)instance)
            {
                TrimObjectValues(element);
            }
            return;
        }
    
        foreach (PropertyInfo prop in props)
        {
            if (prop.PropertyType == typeof(string))
            {
                string value = (string)prop.GetValue(instance, null);
                if (value != null)
                {
                    value = value.Trim();
                    prop.SetValue(instance, value, null);
                }
            }
            else
            {
                TrimObjectValues(prop.GetValue(instance, null));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a look at the following code: class A(object): defaults = {'a': 1} def
I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
I have the following function that is pulling data from a database. The ajax
Given any object with members that look like this: target { x1 : 1
Have a look at the following code. The goal here is to return a
I need to sort string, and I came up with the following function. def
can someone help with this? I need the following function to do this... $x
I have the following jQuery which I need adapting: $(document).ready(function(){ $(.rss-popup a).hover(function() { $(this).next(em).stop(true,
The following function System.Threading.Thread.Sleep(); delay the thread in millisecond, and take the integer value
/** The following function checks the red black tree black height * @param n

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.