LoginSignup
2
4

More than 1 year has passed since last update.

ExpandoObjectクラスを使ってみた

Last updated at Posted at 2021-06-12

ExpandoObjectクラスを使ってみた

業務の中で使用する場面があったので、備忘録として残しておきます。

どんなクラス?

「System.Dynamic」名前空間のクラス。事前に定義していない型のオブジェクトに対して、動的にプロパティを設定することができる。

実際に使ってみた

ExpandoObjectのインスタンスを生成し,動的にプロパティを設定

using System;
using System.Dynamic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //ExpandoObjectクラスのインスタンスを生成
            dynamic obj = new ExpandoObject();

            //プロパティを設定
            obj.Height = 100;
            obj.Width = 50;

            //各プロパティを出力
            Console.WriteLine("Height: {0}", obj.Height);
            Console.WriteLine("Width: {0}", obj.Width);
        }
    }
}

出力結果

Height: 100
Width: 50
Apple
Strawberry
Grapes
30
40

JsonにSeriaizeしてみた

ExpandoObjectで生成したオブジェクトをJsonにSerializeする。

using System;
using System.Collections.Generic;
using System.Dynamic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //ExpandoObjectクラスのインスタンスを生成
            dynamic obj = new ExpandoObject();

            //List
            var list = new List<string>();
            list.Add("Apple");
            list.Add("Strawberry");
            list.Add("Grapes");

            //Dictionary
            var dict = new Dictionary<string, int>();
            dict.Add("Apple", 400);
            dict.Add("Strawberry", 600);
            dict.Add("Grapes", 500);

            //プロパティを設定
            obj.Height = 100;
            obj.Width = 50;
            obj.fruits = list;
            obj.people = dict;

            //各プロパティを出力
            Console.WriteLine("Height: {0}", obj.Height);
            Console.WriteLine("Width: {0}", obj.Width);
            Console.WriteLine(obj.fruits[0]);
            Console.WriteLine(obj.fruits[1]);
            Console.WriteLine(obj.fruits[2]);
            Console.WriteLine(obj.people["taro"]);
            Console.WriteLine(obj.people["jiro"]);
        }
    }
}

出力結果

{"fruits":{"Apple":400,"Strawberry":600,"Grapes":500}}

どんなJsonでもDeserializeしてみたい

構造がわからないJsonをExpandoObjectを使ってDeserializeする。
sample.json

{
    "Name": "Taro",
    "Age": 30,
    "Country": "Tokyo",
    "Skill": [
        "Football",
        "Guitar"
    ]
}
using Newtonsoft.Json;
using System;
using System.Dynamic;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Jsonファイル読み込み
            var sr = new StreamReader(@"C:\work\sample.json");
            var json = sr.ReadToEnd();

            //ExpandoObjectクラスのインスタンスを生成
            dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json);

            //プロパティ出力
            Console.WriteLine(obj.Name);
            Console.WriteLine(obj.Age);
            Console.WriteLine(obj.Country);
            Console.WriteLine(string.Join(",", obj.Skill));
        }
    }
}

出力結果

Taro
30
Tokyo
Football,Guitar

さいごに

今後の業務でもお世話になりそうです。忘れたら見直そう~

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4