Sign in to follow this  
Plerion

Reading from a MemoryStream

Recommended Posts

Plerion    381
Hello! Im trying to make a class from which its possible to read arbitrary types using a generic Read-function. This looks like that:
		generic<typename T>
		void Read(T% in)
		{
			Type^ t = T::typeid;
			if(t->Equals(System::String::typeid))
			{
				in = (T)System::Text::Encoding::ASCII->GetString(m_content, m_readPoint, m_content->Length - m_readPoint);
				m_readPoint += ((String^)in)->Length + 1;
			}
			else
			{
				System::Runtime::Serialization::IFormatter^ fmt = gcnew System::Runtime::Serialization::Formatters::Binary::BinaryFormatter();
				array<unsigned char>^ buffer = gcnew array<unsigned char>(System::Runtime::InteropServices::Marshal::SizeOf(in));
				m_reader->Read(buffer, 0, buffer->Length);
				in = (T)fmt->Deserialize(gcnew System::IO::MemoryStream(buffer));
				m_readPoint += buffer->Length;
			}
		}

There seems to be a problem with that. It works well if i read a System::String but the other types seem not to be right. Deserialize throws an exception that the end of the stream was reached. I found that for a UInt16 the Deserialisation tries to read 53 Bytes from the stream. Whats the reason for that? Greetings Plerion

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this