Implemented the following extension method that creates a dynamic object with variable property names. Basically, it takes any object and turns it into a dynamic object:
public static ExpandoObject ObjetoAnonimo(this object obj)
{
var retorno = new ExpandoObject();
foreach (var property in ReflectionUtils.ExtrairPropertiesDeObjeto(obj).Where(x => !x.GetGetMethod().IsVirtual))
{
var columnAttribute = ReflectionUtils.ExtrairAtributoColumnDeProperty(property);
var nomePropriedade = columnAttribute != null ? columnAttribute.Name : property.Name;
switch (property.PropertyType.ToString())
{
case "System.Int32":
if (Convert.ToInt32(property.GetValue(obj, null)) > 0)
{
((IDictionary<string, object>)retorno).Add(nomePropriedade, property.GetValue(obj, null));
}
break;
case "System.Int64":
if (Convert.ToInt64(property.GetValue(obj, null)) > 0)
{
((IDictionary<string, object>)retorno).Add(nomePropriedade, property.GetValue(obj, null));
}
break;
case "System.DateTime":
if (Convert.ToDateTime(property.GetValue(obj, null)) > DateTime.MinValue)
{
((IDictionary<string, object>)retorno).Add(nomePropriedade, property.GetValue(obj, null));
}
break;
default:
if (property.GetValue(obj, null) != null)
{
((IDictionary<string, object>)retorno).Add(nomePropriedade, property.GetValue(obj, null));
}
break;
}
}
return retorno;
}
For your case, just fill nomePropriedade
with the name of the desired resource.
Usage:
var objeto = new { ColunaResource1 = valor1, ColunaResource2 = valor2, ... };
var objJson = objeto.ObjetoAnonimo();