2011-03-21 3 views
0

두 개의 인수 모델 유형 및 스트림 형식을 받아들이는 메서드를 사용해야합니다.스트림 데이터를 캡처하는 방법은 무엇입니까? C# I/O 기본

public static void Write(Stream stream, Model model); 

먼저 유형 스트림 변수를 만들고 문자열에 스트림에 쓰여진 것을 캡처 한 다음 데이터베이스에 저장하고 싶습니다. Stream 클래스는 그것을 오버라이드하는 방법을 모르는 추상 클래스라는 것을 알게되었습니다.

아무도 제안 할 수 있습니까?

답변

0

당신은 MemoryStream 같은 파생 클래스의 일부를 사용할 수 있습니다

using (var stream = new MemoryStream()) 
{ 
    // pass the memory stream to the method which will write to it 
    SomeClass.Write(stream, someModel); 

    // convert the contents to string using the default encoding 
    string result = Encoding.Default.GetString(stream.ToArray()); 

    // TODO: do something with the result 
} 
+0

덕분에 많이 대린합니다. –

관련 문제