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

  • SEARCH
  • Home
  • 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 240519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:40:24+00:00 2026-05-11T20:40:24+00:00

I am trying to reuse an existing code … but with no success .

  • 0

I am trying to reuse an existing code … but with no success . Here is the code snippet:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Reflection;

namespace GenApp.Utils.Reflection
{
    class FieldTraverser
    {


        public static string SearchFieldValue(object obj, int MaxLevel, string strFieldMeta , ref object fieldValue)
        {
            if (obj == null)
                return null;
            else
            {
                StringBuilder sb = new StringBuilder();
                bool flagShouldStop = false; 
                FieldTraverser.PrivDump(sb, obj, "[ObjectToDump]", 0, MaxLevel , ref flagShouldStop , ref fieldValue);
                return sb.ToString();
            }
        } //eof method 



        public static object GetFieldValue(object obj, string fieldName, ref bool flagShouldStop, ref object objFieldValue)
        {
            FieldInfo fi;
            Type t;

            t = obj.GetType();
            fi = t.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (fi == null)
                return null;
            else
            {

                if (fi.Name.Equals(fieldName))
                {
                    objFieldValue = fi.GetValue(obj);
                    flagShouldStop = true;
                }
                return fi.GetValue(obj);
            } //eof else 
        } //eof method 


        protected static void DumpType(string InitialStr, StringBuilder sb, object obj, 
            int level, System.Type t, int maxlevel , ref bool flagShouldStop , ref object objFieldValue
            )
        {
            FieldInfo[] fi;
            fi = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (t == typeof(System.Delegate)) return;
            foreach (FieldInfo f in fi)
            {
                PrivDump(sb, f.GetValue(obj), f.Name, level + 1, maxlevel , ref flagShouldStop , ref objFieldValue);
                if (flagShouldStop == true)
                    return; 
            }
            object[] arl;
            int i;
            if (obj is System.Array)
            {
                try
                {
                    arl = (object[])obj;
                    for (i = 0; i < arl.GetLength(0); i++)
                    {
                        PrivDump(sb, arl[i], "[" + i + "]", level + 1, maxlevel, ref flagShouldStop, ref objFieldValue);
                        if (flagShouldStop == true)
                            return; 
                    }
                }
                catch (Exception) { }
            }
        }




        protected static void PrivDump(StringBuilder sb, object obj, string objName, int level, int MaxLevel, ref bool flagShouldStop, ref object objFieldValue)
        {

            if (obj == null)
                return;
            if (MaxLevel >= 0 && level >= MaxLevel)
                return;

            string padstr;
            padstr = "";
            for (int i = 0; i < level; i++)
                if (i < level - 1)
                    padstr += "|";
                else
                    padstr += "+";
            string str;
            string[] strarr;
            Type t;
            t = obj.GetType();
            strarr = new String[7];
            strarr[0] = padstr;
            strarr[1] = objName;
            strarr[2] = " AS ";
            strarr[3] = t.FullName;
            strarr[4] = " = ";
            strarr[5] = obj.ToString();
            strarr[6] = "\r\n";
            sb.Append(String.Concat(strarr));
            if (obj.GetType().BaseType == typeof(ValueType))
                return;
            FieldTraverser.DumpType(padstr, sb, obj, level, t, MaxLevel, ref flagShouldStop , ref objFieldValue);
            Type bt;
            bt = t.BaseType;
            if (bt != null)
            {
                while (!(bt == typeof(Object)))
                {
                    str = bt.FullName;
                    sb.Append(padstr + "(" + str + ")\r\n");
                    FieldTraverser.DumpType(padstr, sb, obj, level, bt, MaxLevel , ref flagShouldStop , ref objFieldValue);
                    bt = bt.BaseType;
                    if (bt != null)
                        continue;
                    break;
                } while (bt != typeof(Object)) ;
            }
        } //eof method 
    }//eof class 
    } //eof namespace 
  • 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-11T20:40:25+00:00Added an answer on May 11, 2026 at 8:40 pm

    Note that it is very rare you need to mess with FieldInfo; fields are rarely public, and you should generally be using the PropertyInfos (GetProperties()). However, GetFields will work. For public fields, just GetFields(); for private fields too you need BindingFlags:

    class Foo {
        public string abc;
    }
    class Bar : Foo {
        private int def;
    }
    static class Program {
        static void Main() {
            object obj = new Bar();
            FieldInfo[] fields = obj.GetType().GetFields(
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
    
            foreach(FieldInfo field in fields) {
                Console.WriteLine(field.Name + " = " + field.GetValue(obj));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
I'm trying to reuse parts of some JQuery code I've used before for another
I'm trying to reuse some script that I have working on another page, but
Trying to make a make generic select control that I can dynamically add elements
I'm trying to write code that will load an image from a resource, and
This may be a ridiculous question for you C# pros but here I go.
I'm using Delphi (7-2010) and trying to figure out a good way to handle
I'm trying to test some code in different situations (for different result sets). I've
I'm new to BDD, and using SpecFlow I'm trying to work out an efficient,
I'm trying to build a C++ extension for python using swig. I've followed the

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.