lundi 7 août 2017

Get Name of Declaring Property in Object

I am trying to implement a localization logic using json to store localized string in the database using Entity Framework, but I need help completing the object.

Here's my localized field

[ComplexType]
    public class MultiLanguageField : Dictionary<string, string>
    {
        public void AddRange(Dictionary<string, string> collection)
        {
            foreach (var item in collection)
            {
                this.Add(item.Key, item.Value);
            }
        }       

        [Column("Name")]
        public string Serialized
        {
            get { return JsonConvert.SerializeObject(this); }
            private set
            {
                if(string.IsNullOrEmpty(value))
                {
                    Clear();
                    return;
                }

                var items = JsonConvert.DeserializeObject<MultiLanguageField>(value);
                Clear();
                AddRange(items);
            }
        }
    }

Then I just declare the property in an object like this.

public MultiLanguageField LocalizedField { get; set; }

I want to be able to replace the Column attribute over the Serialized method so the database field takes the name of the declaring property (LocalizedField in this case).

I looked at reflection, but can't manage to get the name of the declaring property.

Any help appreciated. If you know a better way to manage localization in database with Entity Framework, I welcome your input!

Thx





Aucun commentaire:

Enregistrer un commentaire