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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:37:55+00:00 2026-05-16T21:37:55+00:00

In C# 3.0 you can create anonymous class with the following syntax var o1

  • 0

In C# 3.0 you can create anonymous class with the following syntax

var o1 = new { Id = 1, Name = "Foo" };

Is there a way to dynamic create these anonymous class to a variable?


Example:

var o1 = new { Id = 1, Name = "Foo" };
var o2 = new { SQ = 2, Birth = DateTime.Now };

Dynamic create Example:

var o1 = DynamicNewAnonymous(new NameValuePair("Id", 1), new NameValuePair("Name", "Foo"));
var o2 = DynamicNewAnonymous(new NameValuePair("SQ", 2), new NameValuePair("Birth", 
DateTime.Now));

Beacuse I need to do:

dynamic o1 = new ExpandObject(); 
o1."ID" = 1;    <--"ID" is dynamic name
o1."Name" = "Foo";  <--"Name" is dynamic name

And Scene1:

void ShowPropertiesValue(object o)
{
  Type oType = o.GetType();
  foreach(var pi in oType.GetProperties())
  {
    Console.WriteLine("{0}={1}", pi.Name, pi.GetValue(o, null));
  }
}

if I call:

dynamic o1 = new ExpandObject();
o1.Name = "123";
ShowPropertiesValue(o1);

It can’t show the result:

Name = 123

And also I how to Convert the ExpandoObject to AnonymouseType ?

Type type = o1.GetType();
type.GetProperties();   <--I hope it can get all property of o1

Last, I modify ShowPropertiesValue() method

void ShowPropertiesValue(object o)
{
  if( o is static object ) <--How to check it is dynamic or static object?
  {
    Type oType = o.GetType();
    foreach(var pi in oType.GetProperties())
    {
      Console.WriteLine("{0}={1}", pi.Name, pi.GetValue(o, null));
    }
  }
  else if( o is dynamic object )  <--How to check it is dynamic or static object?
  {
    foreach(var pi in ??? )  <--How to get common dynamic object's properties info ?
    {
      Console.WriteLine("{0}={1}", pi.Name, pi.GetValue(o, null));
    } 
  }
}

How to implement DynamicNewAnonymous method or how to modify the ShowPropertiesValue()?

My motivations is:

dynamic o1 = new MyDynamic();
o1.Name = "abc";
Type o1Type = o1.GetType();
var props = o1Type.GetProperties(); <--I hope can get the Name Property

If i can hook dynamicObject’s GetType Method, and Compel convert to strongly-typed Type.
The above Seamless code can work fine.

  • 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-16T21:37:55+00:00Added an answer on May 16, 2026 at 9:37 pm

    Anonymous types are just regular types that are implicitly declared. They have little to do with dynamic.

    Now, if you were to use an ExpandoObject and reference it through a dynamic variable, you could add or remove fields on the fly.

    edit

    Sure you can: just cast it to IDictionary<string, object>. Then you can use the indexer.

    You use the same casting technique to iterate over the fields:

    dynamic employee = new ExpandoObject();
    employee.Name = "John Smith";
    employee.Age = 33;
    
    foreach (var property in (IDictionary<string, object>)employee)
    {
        Console.WriteLine(property.Key + ": " + property.Value);
    }
    // This code example produces the following output:
    // Name: John Smith
    // Age: 33
    

    The above code and more can be found by clicking on that link.

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

Sidebar

Related Questions

In C# 3.0 you can create anonymous class with the following syntax var o
I can create the following and reference it using area[0].states[0] area[0].cities[0] var area =
Is there a way I can create Windows firewall exceptions in VB2005? I'm trying
In Postgresql you can create additional Aggregate Functions with CREATE AGGREGATE name(...); But this
Is there anyway I can create a not in clause like I would have
Is there a website I can create an HTML final four bracket? Or is
Can anybody explain the working of following code...? interface myInterface{} public class Main {
In C# 2.0, I can do the following: public class MyClass { delegate void
I have an the following class I'm trying to create a list of with
I can create a menu item in the Windows Explorer context menu by adding

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.