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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:38:22+00:00 2026-06-09T00:38:22+00:00

Say I have the following: public class MyContainer { public string ContainerName { get;

  • 0

Say I have the following:

public class MyContainer
{
    public string ContainerName { get; set; }
    public IList<Square> MySquares { get; set; }
    public IList<Circle> MyCircles { get; set; }
    public MyContainer()
    {
        MySquares = new List<Square>();
        MyCircles = new List<Circle>();
    }
}

public class Shape
{
    public int Area { get; set; }
}

public class Square : Shape
{
}

public class Circle : Shape
{
}

And now I have a function like so:

private static void Collect(MyContainer container)
{
    var properties = container.GetType().GetProperties();
    foreach (var property in properties)
    {
        if (property.PropertyType.IsGenericType &&
            property.PropertyType.GetGenericTypeDefinition() == typeof(IList<>) &&
            typeof(Shape).IsAssignableFrom(property.PropertyType.GetGenericArguments()[0]))
        {
            var t = property.GetValue(container, null) as List<Square>;
            if (t != null)
            {
                foreach (Shape shape in t)
                {
                    Console.WriteLine(shape.Area);
                }
            }
        }
    }

This works the way I want when I get to the MySquares property, however I was hoping to cast the following way instead:

var t = property.GetValue(container, null) as List<Shape>;

I was hoping that it would cycle through all of MyContainer’s properties that have a similar list. Any way I can do this?

  • 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-06-09T00:38:24+00:00Added an answer on June 9, 2026 at 12:38 am

    As I suggested in my comment, using a covariant interface allows you to accomplish this.

    Covariance/Contravariance: http://msdn.microsoft.com/en-us/library/ee207183.aspx
    See also: Covariance and IList

    Working Sample

    namespace Covariance
    {
        public class MyContainer
        {
            public string ContainerName { get; set; }
            public IList<Square> MySquares { get; set; }
            public IList<Circle> MyCircles { get; set; }
            public MyContainer() {
                MySquares = new List<Square>();
                MyCircles = new List<Circle>();
            }
        }
    
        public class Shape
        {
            public int Area { get; set; }
        }
    
        public class Square : Shape
        {
        }
    
        public class Circle : Shape
        {
        }   
    
        class Program
        {
            static void Main( string[] args ) {
    
                MyContainer mc = new MyContainer();
                mc.MyCircles.Add( new Circle { Area = 60 } );
    
                Collect( mc );
                Console.ReadLine();
            }
    
            private static void Collect( MyContainer container ) {
                var properties = container.GetType().GetProperties();
                foreach( var property in properties ) {
                    if( property.PropertyType.IsGenericType &&
                        property.PropertyType.GetGenericTypeDefinition() == typeof( IList<> ) &&
                        typeof( Shape ).IsAssignableFrom( property.PropertyType.GetGenericArguments()[0] ) ) {
                        var t = property.GetValue( container, null ) as IEnumerable<Shape>;
                        if( t != null ) {
                            foreach( Shape shape in t ) {
                                Console.WriteLine( shape.Area );
                            }
                        }
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following class: public MainFormViewModel { public String StatusText {get; set;}
Say i have the following: public class Person{ public int ID {get; set;} public
ok say i have the following model: public class bar{ public string bar {get;
Say you have the following object: public class Address { public String Line1 {
Let's say I have following POJO's using Hibernate. public class User { private String
Lets say i have following class public class abc { int id; string name;
Say I have the following classes: public class A { public string x; }
lets say I have the following public class A { private string _someField; public
Say I have the following classes public class TestA { public string Blah {
Let's say I have the following classes : public class MyProductCode { private String

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.