2011-03-12 7 views
3

나는 linq 클래스와 인터페이스 클래스가 있고 System.Data.Linq.Binary 데이터 형식을 사용합니다.C# 일반 주조 Linq 함께

// .Value is a System.Data.Linq.Binary DataType 
public class DataType<T> where T : class 
{ 
    public T Value 
    { 
     get 
     { 
      return from d in Database 
        where d.Value = [Some Argument Passed] 
        select d.Value as T; 
     } 
    } 
} 

public class StringClass : DataType<string> 
{ 
} 

public class ByteClass : DataType<byte[]> 
{ 
} 

StringClass.Value이 제대로 캐스팅 데이터베이스에서 string을 반환 : 나는 바이너리로 저장되어있는 데이터 유형을 의미하는 일반 배열을 취하는 간단한 클래스를 작성하려고 해요?

ByteClass.Value 정확하게 캐스팅하여 byte[]을 데이터베이스에서 반환 하시겠습니까?

내 주요 질문은 기본적으로 System.Data.Linq.Binary를 사용할 수있는 방법을 해결합니다.

편집 : System.Data.Linq.Binary를 T로 변환하려면 어떻게해야합니까? T는 무엇이든 될 수 있습니다. Binary를 T로 변환 할 수 없기 때문에 코드가 실제로 작동하지 않습니다. 당신이

System.Data.Linq.Binary b1; 

string str = b as string; 

System.Data.Linq.Binary b2 

byte[] bArray = b2 as byte[]; 

을하고 본질적으로

답변

1

모두 STR과 bArray는 null가됩니다;

당신은

public class DataType<T> where T : class 
{ 
    public T Value 
    { 
     get 
     { 
      // call ConvertFromBytes with linqBinary.ToArray() 
      // not sure about the following; you might have to tweak it. 
      return ConvertFromBytes((from d in Database 
        where d.Value = [Some Argument Passed] 
        select d.Value). 
      First().ToArray()); 
     } 
    } 

    protected virtual T ConvertFromBytes(byte[] getBytes) 
    { 
     throw new NotImplementedException(); 
    } 
} 

public class StringClass : DataType<string> 
{ 
    protected override string ConvertFromBytes(byte[] getBytes) 
    { 
     return Encoding.UTF8.GetString(getBytes); 
    }  
} 

public class ByteClass : DataType<byte[]> 
{ 
    protected override byte[] ConvertFromBytes(byte[] getBytes) 
    { 
     return getBytes; 
    } 
} 
뭔가를해야합니다