• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • C#

ExpandoObject in C#

  • Bhavik Sarvaiya
  • August 4, 2017
  • 2 minute read
csharp
Total
0
Shares
0
0
0

In this article, I would like to share extensible dynamic object in C#, we all have idea about dynamic and extensible object in scripting language. C# .net 4.0 introducing dynamic type that provides a host of dynamic features for the static C# language. With the dynamic keyword, one interesting object comes out to be ExpandoObject.

ExpandoObject

C# .NET 4.0 has a cool new ExpandoObject class, in the System.Dynamic namespace. It provides very dynamic object that allows you to add properties and methods on the fly and then access them again.

For example, you can do like

dynamic school = new ExpandoObject();

school.Id = 001;

school.Name = "St.Xavier";

//Nested dynamic instance
school.address = new ExpandoObject();

school.address.city = "Gandhinagar";

school.address.state = "Gujarat";

//Bind dynamic Event
school.HelloWorld = (Func<string, string>)((string name) =>
{
  return "Hello " + name;
});

Console.WriteLine(school.Name);
Console.WriteLine(school.HelloWorld(".net"));

Real time use:

Before send rest call response if you want to perform any data structure modification like split data in two different formats (no need makes separate class)

Before send rest call response if you want to set hierarchical data structure of your multiple query output. E.g.:

Query-1 output with list of state and Query-2 output with list of cities. If you want set output in state>>city hierarchical structure like as below it just possible without making separate class.

<State>
    <id>1</id>
    <name>Gujarat</name>
    <city>
        <iscapital>true</iscapital>
        <City_Name>Ganhinagar</City_Name >
    </city>
    <city>
        <iscapital>false</iscapital>
        <City_Name>Bhavnagar</City_Name >
    </city>
</state>

As cool as ExpandoObject is it has a few limitations too:

  • It is a sealed type so you can not use it as a base class
  • It only works off ‘properties’ in the internal Dictionary – you can’t expose existing type data

Click Here to explore all the technology categories on which our authors write an articles

Bhavik Sarvaiya
Bhavik Sarvaiya

As .Net developer 5+ years of experience , having knowledge different Microsoft technologies and scripting framework like C#, Asp.Net, jQuery, SQL Server, ADO.Net, XML Processing, angular, MVC, WEB API, WCF. Knowledge and industry experience with complete software development life cycles: analyzing the requirements, designing, and writing of implementation specifications, coding, testing. Good skills in preparing project timeline and providing project estimation and efforts.
I love to write complex logic and provide solution.

Views: 6,750

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0
2 comments
  1. xml says:
    March 20, 2019 at 5:52 pm

    How can we read an xml into C#? is there any xml tutorial where we can get reference?
    Thanks in advance.

    Reply
    1. Mr. Bill says:
      August 30, 2019 at 7:16 pm

      Yes, you can use this reference to read xml using jquery. They have explained good with an example. I hope this will help you.
      Thanks

      Reply

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • Getting Started with AWS Step Functions: Orchestration Made Simple

    Getting Started with AWS Step Functions: Orchestration Made Simple

    1 month ago
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    2 years ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

%d