0

I have the class structure:

public class Task1 : ITask

and

public class Task2 : ITask

and some method:

public static T FromByteArray<T>(byte[] data)
        {
            if (data == null)
                return default(T);            
            using (var stream = new MemoryStream(data))
            using (var reader = new StreamReader(stream, Encoding.UTF8))
                return (T)JsonSerializer.Create().Deserialize(reader, typeof(T));                
        }

where I pass the interface. And get the error: "Could not create an instance of type ITask. Type is an interface or abstract class and cannot be instantiated." I read similar questions, but how can I use the solution with the JsonSerializerSettings in my case? In my case method Deserialize do not have an option for JsonSerializerSettings

Dmytro
  • 159
  • 1
  • 1
  • 7
  • interfaces are non serializable. – rdr20 Mar 18 '21 at 08:47
  • If your question is *How can I deserialize from a stream using `JsonSerializerSettings`*, then use `JsonSerializer.CreateDefault(settings)` or `JsonSerializer.Create(settings)` as shown in e,g. [this answer](https://stackoverflow.com/a/32944462/3744182) to [Can I decompress and deserialize a file using streams?](https://stackoverflow.com/q/32943899/3744182). Or is your problem that you cannot modify `FromByteArray(byte[] data)` in any way? If so, what can you modify? – dbc Mar 18 '21 at 17:00
  • Yes, I solve problem, thank you – Dmytro Mar 19 '21 at 10:23

0 Answers0