Set anonymous object property name and resources

1

I'm using multi-language resources on a system.

In a json return I create an anonymous object

var resultado = minhalista.Select(x => new { })

But I happen to need the property of this anonymous object to be according to the value of the resource, because my View expects the name according to the resource

I mean, I know it does not ... but it would be something like:

var resultado = minhalista.Select(x => new { Resource.Nome = x.Nome })

Then the property must come, Name, Name, Name..etc according to the value of the resource

Is there any way to do this?

    
asked by anonymous 08.04.2015 / 02:47

2 answers

1

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();
    
08.04.2015 / 17:59
0

Rod,

I think the most recommendable way would be to type your variable result.

Example:

class Person 
{ 
    public String Name{get;set;} 
    public String Email{get;set;} 
}

var resultado = (from item in minhalista select new Person{ Name = item.Name, Email = item.Email }).ToList();

There is an object called ExpandoObject ( System.dynamic ) but I do not know if it is possible to be serialized via Json

    
08.04.2015 / 16:37