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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:36:33+00:00 2026-06-17T20:36:33+00:00

public class Peploe { public string Name { get; set; } } public class

  • 0
public class Peploe
{
    public string Name { get; set; }
}

public class Animal
{
    public string NickName { get; set; }
}

internal static class Program
{
    /// <summary>
    /// This 'ItemSorce' will be assignment by anywhere , so i don't know it have 'Name' property.
    /// </summary>
    public static IEnumerable ItemSource { get; set; }

    private static void Main()
    {
        var list = new List<Peploe>() {new Peploe() {Name = "Pato"}};
        ItemSource = list;

        //Test2
        //var animals = new List<Animal>() { new Animal() { NickName = "Pi" } };
        //ItemSource = animals;

        dynamic dy;
        foreach (var item in ItemSource)
        {
            dy = item;
            Console.WriteLine(dy.Name);//If I Uncomment 'Test2',it will throw a RuntimeBinderException at here.
        }
    }
}

If I use reflection,it can resolve this problem. But when ‘ItemSource’ is very huge, the ‘foreach’ will excute many times,the performance is bad.How can I resolve this problem.

  • 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-17T20:36:34+00:00Added an answer on June 17, 2026 at 8:36 pm

    You need to add little bit of reflection to make is complete dynamic. And trust me that will not hurt performance as I am already using it. Here is code sample I have created from you sample. It is still not production ready but you will get the basic idea how you can do it, with all your restriction.

    dynamic dy;
                List<dynamic> result = new List<dynamic>(); 
    
                foreach (var item in ItemSource)
                {
                    dy = new ExpandoObject();
                    var d = dy as IDictionary<string, object>;
    
                    foreach (var property in item.GetType().GetProperties())
                    {
                        d.Add(property.Name, item.GetType().GetProperty(property.Name).GetValue(item, null));
                    }
    
                    result.Add(dy);
                }
    
                foreach (var item in result)
                {
                    var r = ((dynamic)item) as IDictionary<string, object>;
                    foreach (var k in r.Keys)
                    {
                        Console.WriteLine(r[k] as string);
                    }
                }
    

    This code works exactly way you want. Its not depended on whatever property you have in class. Please let me know if any further details needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Emp { public int ID { get; set; } public string Name
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class Reverse { public static void main(String[] args) { char name[]=prakash.toCharArray();////initialisng array ////////swapping
public class MyWebControl { [ExternallyVisible] public string StyleString {get;set;} } public class SmarterWebControl :
public class Customer { public int CustomerId { get; set; } public string FirstName
public class Foo { public string Bar {get; set;} } How do I get
public class FontType { ... public String Name { get { return _name; }
public class NewCallInfo { public string Site { get; set; } public string CustomerName
public class Address{ public string ContactName {get; private set;} public string Company {get; private
public class Abbreviation { public string ShortName { get; set; } public string LongName

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.