I implemented the following extension method that creates a dynamic object with variable property names. Basically, it takes any object and transforms 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;
}
In your case, just fill out nomePropriedade
with the desired Resource name.
Use:
var objeto = new { ColunaResource1 = valor1, ColunaResource2 = valor2, ... };
var objJson = objeto.ObjetoAnonimo();
Why does the property need to be located? This should be an internal piece of software information and should not need localization. There’s always a way but it can be so complicated that it’s not worth the effort.
– Maniero
@bigown is because of a js function that generates the grid already with the head, and other settings
– Rod
I don’t know all the mechanics of what you’re using but it shouldn’t depend on property, there should be another way to get this information.
– Maniero
There is a property in jquery datatables that is Columns, it is passed like this: { "data": "Name", }, this is what the plugin expects from json, so it expects "Name", in my function i Gero this and also thead from table, if it really has no way, the way is to change my function in js even then, rs
– Rod
There’s a way, but it’s so complicated, so out of character, it’s usually better to find another way. Unless the other one’s even more complicated :)
– Maniero
@bigown I believe that change js is not so complicated, I will change, thanks for the answers...
– Rod