How to store a System.Collections.Generic.Dictionary in realm-dotnet

旧时模样 提交于 2021-02-08 04:41:05

问题


I m trying to integrate Realm .NET in my uwp project and I would like to know if there is any way to store a System.Collections.Generic.Dictionary in realm-dotnet base. I tried this :

 public class Training : RealmObject
{
    public int Id { get; set; }
    public int Id_document { get; set; }
    public IDictionary<string, string>  Names { get; set;}
} 

But I m getting this error :

Error Fody/RealmWeaver: Training.Name is a 'System.Collections.Generic.IDictionary`2' which is not yet supported.

Thanks for advance,


回答1:


Base request

There isn't a way to directly store a System.Collections.Generic.Dictionary into Realm.
On the Realm official website you can find all the informations that you're searching, as an example, here you can find the full list of the supported data types (Collections too).

In order to save data with a structure as kind as a Dictionary, you would want to use a custom class that inherits from RealmObject with two IList<T> properties, such as:

public class RealmDictionary : RealmObject {
    public IList<T> Keys { get; }
    public IList<T> Values { get; }
}

An in your class where you want to store the Dictionary, use an instance of the above RealmDictionary class:

...
public RealmDictionary Names { get; set; }
...

Update

(Using NewtonsoftJson)
If you must serialize the data into a Json string, you can setup your base class as:

...
[JsonIgnore]
public RealmDictionary RealmNames { get; set; }
public Dictionary<string,string> Names {
    get {
        return RealmNames.AsDictionary
    }   
}
...

Then adding into the RealmDictionary class a property/method that constructs a dictionary with the values, something along the lines..

public Dictionary<string,string> AsDictionary {
    get {
        Dictionary<string,string> temp = new Dictionary<>();
        for(int i = 0; i < Keys.Count; i++){
            temp.Add(Keys[i], Values[i]);
        }
        return temp;
    }
}



回答2:


This throws an error where it cant find "T"

public class RealmDictionary : RealmObject 
{
    public IList<T> Keys { get; }
    public IList<T> Values { get; }
}

The correct way of doing this is to add a <T> in front of the class name if you want both the keys and values to be of the same type.

public class RealmDictionary<T> : RealmObject 
{
    public IList<T> Keys { get; }
    public IList<T> Values { get; }
}

If you want the key to be of a different type than the value then it would look something like

public class RealmDictionary<T, K> : RealmObject 
{
        public IList<T> Keys { get; }
        public IList<K> Values { get; }
}

Also for the same type of keys and values the implementation

public RealmDictionary Names { get; set; }

would actually throw an exception.

Right way to do it would be

public RealmDictionary<"TYPE THAT YOU WANT"> Names { get; set; }

For eg.

public RealmDictionary<string> Names { get; set; }

For different type of keys and values the implementation looks like this

public RealmDictionary<string, object> Names { get; set; }

So your Keys will be of type String and the Values it maps to will be of type Object



来源:https://stackoverflow.com/questions/49150084/how-to-store-a-system-collections-generic-dictionary-in-realm-dotnet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!