Set name property anonymous object and Resources

Asked

Viewed 279 times

1

I am using Resources for multi-language on a system.

In a return json I create an anonymous object

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

But it turns out that I need the property of this anonymous object to be according to the value of Resource, because my View expects the name according to Resource

I mean, I know I don’t... but it would be something like:

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

Then the property should come, Name, Name, Name. etc according to the value of the Resource

Is there any way to do that?

  • 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.

  • @bigown is because of a js function that generates the grid already with the head, and other settings

  • 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.

  • 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

  • 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 :)

  • @bigown I believe that change js is not so complicated, I will change, thanks for the answers...

Show 1 more comment

2 answers

1


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();

0

Rod,

I believe that the most recommended 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 don’t know if the same is possible to be serialized via Json

Browser other questions tagged

You are not signed in. Login or sign up in order to post.