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 6023449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:00:25+00:00 2026-05-23T04:00:25+00:00

I want to loop through my Car class with reflection to remove all null

  • 0

I want to loop through my Car class with reflection to remove all null values and replace them with better values like string = “”.

    [Serializable()]
public class Car
{
    public string model;
    public int year;
    public List<Owner> owner;
}

[Serializable()]
public class Owner
{
    public string firstName;
    public string lastName;
}

I have made this so far

   public void LoopEverythingAndFix(object type)
    {
        var prop = type.GetType().GetFields();

        foreach (var fieldInfo in prop)
        {
            if (GetType(fieldInfo))
            {
                var value = fieldInfo.GetValue(type);

                if (value == null)
                    fieldInfo.SetValue(type, GetDefaultValue(fieldInfo));
            }
            else
            {
                LoopEverythingAndFix(fieldInfo);
            }
        }
    }
    public bool GetType(System.Reflection.FieldInfo fieldInfo)
    {
        if (fieldInfo.FieldType == typeof(string))
            return true;

        if (fieldInfo.FieldType == typeof(bool))
            return true;

        if (fieldInfo.FieldType == typeof(int))
            return true;

        if (fieldInfo.FieldType == typeof(decimal))
            return true;

        return false;
    }

The GetType method is to know if current field is a class like “owner” class or if its a value/reference field like int/string,and if it is a type of “owner” then i want to loop that to and fix those properties as well.

The problem is when it finds “owner” in car class, and goes to execute :

 LoopEverythingAndFix(fieldInfo);

thats where the problem is, because i send fieldInfo to the method LoopEverythingAndFix and when it goes back in the loop it gets 0 fields at type.GetType().GetFields(). It is a list and i want to loop the listitems and send them to the LoopEverythingAndFix method

  • 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-23T04:00:26+00:00Added an answer on May 23, 2026 at 4:00 am

    You’re trying to call LoopEverythingAndFix on the Reflection type FieldInfo rather than the actual object you want to fix.

    To fix, replace this:

    LoopEverythingAndFix(fieldInfo);
    

    With this:

    LoopEverythingAndFix(fieldInfo.GetValue(type));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to loop through a collection of objects and add them all to
i want to loop through all dropdown selects with a certain class name and
Suppose I have a vector<int> myvec and I want to loop through all of
If I have a self-referential Topic class and I want to recursively loop through
I have the following code and i want to loop through all the fields
I want to loop through a databale and change the values of a specific
I want to loop through all the variables stored in the session. I checked
hi i want to loop through some tr's like this . <tr> <td> <input
Suppose we want to loop through all the items in a dropdown list and
I want to loop through a xml file and display the values in text

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.