Hi,
ich möchte gerne ein Interface de- sowie serializen.
So sehen meine 2 methoden aus:
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
| public string ConvertEntityContainerToString(List<IEntityType> input) { if (input == null || input.Count <= 0) return null; try { return JsonConvert.SerializeObject(input); } catch (Exception) { return null; } } public List<IEntityType> ConvertStringToEntityContainer(string input) { if (string.IsNullOrEmpty(input)) return null; try { return JsonConvert.DeserializeObject<List<IEntityType>>(input); } catch (Exception) { return null; } } |
Doch dann kommt folgender Fehler:
Newtonsoft.Json.JsonSerializationException was caught
HResult=-2146233088
Message=Could not create an instance of type x.IEntityType. Type is an interface or abstract class and cannot be instantiated. Path '[0].Service'
So sieht das Interface aus:
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
| public interface IEntityType { string Service { get; set; } string Name { get; set; } List<IProperty> PropertyRefs { get; set; } List<IProperty> Properties { get; set; } List<INavigationProperty> NavProperties { get; set; }
List<string> GetPropNames(); List<string> GetPropRefNames(); List<IProperty> GetAllProperties(); List<string> GetAllPropertiesNames(); string GetTypeByName(string keyName); INavigationProperty GetNavigationPropertyByName(string name); INavigationProperty GetNavigationPropertyByAssociationName(string toEntity); List<IProperty> GetAllMandatoryProperties(); string ToString(); List<IProperty> Definition(); Hashtable GetPropRefWithValue(Hashtable properties); bool RemovePropByName(string name); } |
Habe folgende Lösungen schon versucht:
stackoverflow.com/qu...serialize-properties
Doch scheint einfach nicht zu funktionieren. Wäre sehr nett wenn jemand mir weiterhelfen könnte
EDIT:
Es muss nicht von der Newtonsoft Libary kommen, wenn .Net es schon anbietet ein anderen Serialzer in diesen Falle einwandfrei funktioniert bin ich auch zufrieden (xml auch möglich).