title: How to deserialize byte[] into generic object to be cast at method call
body: <p>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 thing holding me up is the deserialize method. I have the function returning type(object) in the hopes of returning a weak typed object. It works as long as I cast the type during the return value assignment, but If I deserialize into type 'object' the result is byte[]. </p>
  
  <p>This means that I have to write a deserialize method for every object(T) i want to operate on. What I want to do is pass Type(T) as a parameter to the deserialize method so I can deserialize into object(T), then return the typed object.</p>
  
  <p>The problem is that using a typed parameter is apparently not allowed. 
  <br/>If I do <code>obj = (type.GetType())br.Deserialize(ms);</code>
  I get <code>; expected</code> between '(object(T)) and br.'.</p>
  
  <p>If I do <code>obj = (br.Deserialize(ms) as type);</code> I get <code>The type of namespace "type" could not be found. (are you missing a using directive or an assembly reference?)</code></p>
  
  <p>or I get a symbol can not be resolved error. 
  Any help is appreciated. Full code is below.</p>
  
  <pre><code>        private byte[] serialize(object param)
      {
          byte[] encMsg = null;
          using (MemoryStream ms = new MemoryStream())
          {
              IFormatter br = new BinaryFormatter();
              br.Serialize(ms, param);
              encMsg = ms.ToArray();
          }
  
          return encMsg;
      }
  
      private object deserialize(byte[] param)
      {
          object obj = null;
          using (MemoryStream ms = new MemoryStream(param))
          {
              IFormatter br = new BinaryFormatter();
              obj = (br.Deserialize(ms) as myObject);
          }
  
          return obj;
      }
  
      private byte[] encrypt(byte[] param)
      {
          byte[] encMsg = null;
          using (Aes myAes = Aes.Create())
          {
              myAes.Padding = PaddingMode.ANSIX923;
              ICryptoTransform autoBot = myAes.CreateEncryptor(myAes.Key, myAes.IV);
              using (MemoryStream ms = new MemoryStream())
              {
                  using (CryptoStream cs = new CryptoStream(ms, autoBot, CryptoStreamMode.Write))
                  {
                      cs.Write(param, 0, (int)param.Length);
                  }
                  encMsg = ms.ToArray();
              }
          }
          return encMsg;
      }
  
      private byte[] decrypt(byte[] key, byte[] iv, byte[] param)
      {
          byte[] dcparam = null;
          using (Aes myAes = Aes.Create())
          {
              myAes.Padding = PaddingMode.ANSIX923;
              ICryptoTransform autoBot = myAes.CreateDecryptor(key, iv);
              using (MemoryStream ms = new MemoryStream())
              {
                  using (CryptoStream cs = new CryptoStream(ms, autoBot, CryptoStreamMode.Write))
                  {
                      cs.Write(param, 0, (int)param.Length);
                  }
                  dcparam = ms.ToArray();
              }
          }
          return dcparam;
      }
  </code></pre>