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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:05:01+00:00 2026-05-30T16:05:01+00:00

I have a need that is a bit similar to this question , except

  • 0

I have a need that is a bit similar to this question, except that it requires a deeper exploration of the source object.

Here is a code sample:

public class Target {};

public class Analyzed
{
    public Target EasyOne { get; set; }
    public IList<Target> ABitMoreTricky { get; set; }
    public IList<Tuple<string, Target>> Nightmare { get; set; }
}

From an instance of Analyzed, I want to extract all the Target instances.

In order to ease the exploration, we can assume the following:

  1. Explore only properties.
  2. There is no infinite reference loop.

For now, EasyOne is… easy, but I am looking for some strategy to get all the Target instances lost in more tricky structures.

  • 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-30T16:05:04+00:00Added an answer on May 30, 2026 at 4:05 pm

    How about something along these lines:

        public List<T> FindAllInstances<T>(object value) where T : class
        {
    
            HashSet<object> exploredObjects = new HashSet<object>();
            List<T> found = new List<T>();
    
            FindAllInstances(value, exploredObjects, found);
    
            return found;
        }
    
        private void FindAllInstances<T>(object value, HashSet<object> exploredObjects, List<T> found) where T : class
        {
            if (value == null)
                return;
    
            if (exploredObjects.Contains(value))
                return;
    
            exploredObjects.Add(value);
    
            IEnumerable enumerable = value as IEnumerable;
    
            if (enumerable != null)
            {
                foreach(object item in enumerable)
                {
                    FindAllInstances<T>(item, exploredObjects, found);
                }
            }
            else
            {
                T possibleMatch = value as T;
    
                if (possibleMatch != null)
                {
                    found.Add(possibleMatch);
                }
    
                Type type = value.GetType();
    
                PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty);
    
                foreach(PropertyInfo property in properties)
                {
                    object propertyValue = property.GetValue(value, null);
    
                    FindAllInstances<T>(propertyValue, exploredObjects, found);
                }
    
            }
    
        private void TestIt()
        {
            Analyzed analyzed = new Analyzed()
            {
                EasyOne = new Target(),
                ABitMoreTricky = new List<Target>() { new Target() },
                Nightmare = new List<Tuple<string, Target>>() { new Tuple<string, Target>("", new Target()) }
            };
    
            List<Target> found = FindAllInstances<Target>(analyzed);
    
            MessageBox.Show(found.Count.ToString());
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have posted a question on here previously asking similar advise, but this project
I have an encrypted bit of text that I need to decrypt. It's encrypted
I have values that are 64-bit unsigned ints, and I need to store them
I have this application that need to do some things in protected paths (like
Sorry if the question is bit confusing. This is similar to this question I
Similar to this Java question . I would like to specify that a variable
For the record: I found a similar question here but I have to elaborate
I have projects that need to be build with a specific version of the
I have ItemRenderers that need to listen for events. When they hear an event
I have need for a function pointer that takes two arguments and returns a

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.