C# – DynamicBag playing with Dynamic and Polymorphism

DynamicBag is a toy project that used a Dictionary<string, dynamic> to create a dynamic storage class.

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
 
namespace Com.Okmer.DynamicTypes
{
    [Serializable]
    public class DynamicBag : Dictionary<stringdynamic>
    {
        public DynamicBag() { }
 
        public DynamicBag(int capacity) : base(capacity) { }
 
        public DynamicBag(IDictionary<stringdynamic> dictionary) : base(dictionary) { }
 
        public DynamicBag(SerializationInfo info, StreamingContext context) : base(info, context) { }
 
        public void ToBinaryFile(string fileName)
        {
            BinarySerialization.ToFile(this, fileName);
        }
 
        public static DynamicBag FromBinaryFile(string fileName)
        {
            return BinarySerialization.FromFile<DynamicBag>(fileName);
        }
    }
}

These DynamicBag(s) can be serialized to and deserialized from a file using BinarySerialization.

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
 
namespace Com.Okmer.DynamicTypes
{
    public static class BinarySerialization
    {
        public static void ToFile<T>(T value, string fileName) where T : ISerializable
        {
            IFormatter formatter = new BinaryFormatter();
            using (Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                formatter.Serialize(stream, value);
            }
        }
 
        public static T FromFile<T>(string fileName) where T : ISerializablenew()
        {
            T result = new T();
            IFormatter formatter = new BinaryFormatter();
            using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                result = (T)formatter.Deserialize(stream);
            }
            return result;
        }
    }
}

The simple example application uses polymorphism to print the content of the DynamicBag(s) to the console.

using System;
using System.Collections.Generic;
 
using Com.Okmer.DynamicTypes;
 
namespace SampleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            DynamicBag shoppingBag = new DynamicBag();
            DynamicBag spareBag = new DynamicBag();
 
            shoppingBag.Add("spareBag", spareBag);
            shoppingBag.Add("shoppingList"new string[] { "eggs""milk""cheese" });
 
            spareBag.Add("carCoin""50ct");
            spareBag.Add("carValue", 50);
 
            Print(shoppingBag);
 
            Console.Write("---------------------------> ENTER <----: ");
            Console.ReadLine();
 
            shoppingBag.ToBinaryFile("shoppingBag.bin");
 
            DynamicBag shoppingBagFromBinaryFile = DynamicBag.FromBinaryFile("shoppingBag.bin");
 
            Print(shoppingBagFromBinaryFile);
 
            Console.Write("---------------------------> ENTER <----: ");
            Console.ReadLine();
        }
 
        private static void Print(DynamicBag bag)
        {
            Console.WriteLine("BAG BEGIN");
            foreach (string key in bag.Keys)
            {
                Console.WriteLine(key + ": ");
                Print(bag[key]);
            }
            Console.WriteLine("BAG END");
        }
 
        private static void Print(IEnumerable<dynamic> values)
        {
            Console.WriteLine("ENUMERABLE BEGIN");
            foreach (dynamic value in values)
            {
                Print(value);
            }
            Console.WriteLine("ENUMERABLE END");
        }
 
        private static void Print(bool value) => Console.WriteLine("bool -> " + value.ToString());
 
        private static void Print(int value) => Console.WriteLine("int -> " + value.ToString());
 
        private static void Print(double value) => Console.WriteLine("double -> " + value.ToString());
 
        private static void Print(string value) => Console.WriteLine("string -> " + value.ToString());
 
        private static void Print(object value) => Console.WriteLine("object-> " + value.ToString());
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.