I’m trying to make a genric code to serialize and deserialize any object that implement a specific interface. The problem is that I need to create an instance of an object before I can call the .Deserialize() because you can’t use a static function in an interface. Now my Question is how do I make an instance of T ? or is there a better way to achieve my goal ?
public static class Serializer { public static byte[] Serialize<T>(T Obj) where T : Data; public static T Deserialize<T>(byte[] Data) where T : Data { //Here I need to something like: new T().Deserialize(Data); } } public interface Data { public byte[] Serialize<T>(T obj); public T Deserialize<T>(byte[] Data);
}
class Program
{
static void Main(string[] args)
{
Serializer.Deserialize<Dummy>(new byte[20]);
}
}
class Dummy : Data
{
}
Update
Fixed the code/typos
Use the
new()keyword