Dasync.ValueContainer

Used by D-ASYNC serialization.


Keywords
d-async, dasync, serialization, csharp, dynamic-types, generated-code, poco, proxy, tuple, valuetuple
License
MIT
Install
Install-Package Dasync.ValueContainer -Version 2.0.0-beta1

Documentation

This libarary is a part of the Dasync project.

Examples.

  1. Create new type:
using Dasync.ValueContainer;
using NewtonSoft.Json;

// ...

var properties = new []
{
  new KeyValuePair<string, Type>("AccountName", typeof(string)),
  new KeyValuePair<string, Type>("AccessCode", typeof(long))
};

var container = ValueContainerFactory.Create(properties);
container.SetValue(0, "test");
container.SetValue(1, 12345L);

var json = JsonConvert.SerializeObject(container);
// { "AccountName": "test", "AccessCode": 12345 }
  1. Create proxy container type to access possibly hidden members:
using Dasync.ValueContainer;
using NewtonSoft.Json;
using System.Reflection;

public class AccountAccessCode
{
  private string _name;
  private long _code { get; set; }
}

// ...

var nameFieldInfo = typeof(AccountAccessCode).GetTypeInfo().GetDeclaredField("_name");
var codePropertyInfo = typeof(AccountAccessCode).GetTypeInfo().GetDeclaredProperty("_code");

var properties = new []
{
  new KeyValuePair<MemberInfo, string>(nameFieldInfo, "AccountName"),
  new KeyValuePair<MemberInfo, string>(codePropertyInfo, "AccessCode")
};

var accountAccessCode = new AccountAccessCode();
var container = ValueContainerFactory.CreateProxy(accountAccessCode, properties);
container.SetValue(0, "test");
container.SetValue(1, 12345L);


var json = JsonConvert.SerializeObject(container);
// { "AccountName": "test", "AccessCode": 12345 }