How to deserialize byte[] into generic object to be cast at method call

前端 未结 2 1543
隐瞒了意图╮
隐瞒了意图╮ 2021-02-14 19:26

I am working on an object encryption class. I have everything worked out but I want to be able to encrypt/decrypt any object type with one deserialize method. As of now the only

2条回答
  •  [愿得一人]
    2021-02-14 19:44

    The type you want to deserialize must be known at compile time.. So your method can be like:

    private T Deserialize(byte[] param)
    {
        using (MemoryStream ms = new MemoryStream(param))
        {
            IFormatter br = new BinaryFormatter();
            return (T)br.Deserialize(ms);
        }
    }
    

    Now you can use it like

    var myclass = Deserialize(buf);
    

提交回复
热议问题