“System.Collections.Generic.List`1”SerializationBinder BindToType

时间:2012-07-31 17:52:44

标签: c# serializationbinder

我有一个Web服务,它将以下内容复制并作为字节数组返回给客户端:

[Serializable]
public class MyFile
{
  public byte[] Data;
  public string FileName;
}

也就是说,我将 List(Of MyFile)返回给客户端。

客户端使用以下TYPENAME

进行消费
System.Collections.Generic.List`1[[NorwayTaxService.Streaming+MyFile, NorwayTaxService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

,程序集名称为:

mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

我正在使用以下两个类绑定到反序列化:

[Serializable]
public class MyFileLocal : ISerializable
{
  public byte[] Data;
  public string FileName;

  // The security attribute demands that code that calls 
  // this method have permission to perform serialization.
  [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
  void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  {
    info.AddValue("Data", Data);
    info.AddValue("FileName", FileName);
  }

  // The security attribute demands that code that calls   
  // this method have permission to perform serialization.
  [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
  private MyFileLocal(SerializationInfo info, StreamingContext context)
  {
    Data = (byte[])info.GetValue("Data", Data.GetType());
    FileName = info.GetString("FileName");
  }
}

sealed class MyFileWebToLocalVersionBinder : SerializationBinder
{
  public override Type BindToType(string assemblyName, string typeName)
  {
    Type typeToDeserialize = null;

    // For each assemblyName/typeName that you want to deserialize to 
    // a different type, set typeToDeserialize to the desired type.
    String assemVer1 = Assembly.GetExecutingAssembly().FullName;
    String typeVer1 = "NorwayTax.Streaming+MyFile";

    if (assemblyName == assemVer1 && typeName == typeVer1)
    {
      // To use a type from a different assembly version,  
      // change the version number. 
      // To do this, uncomment the following line of code. 
      // assemblyName = assemblyName.Replace("1.0.0.0", "2.0.0.0");

      // To use a different type from the same assembly,  
      // change the type name.
      typeName = "MyFileLocal";
    }


    typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));

    return typeToDeserialize;
  }
}

但是对于我的生活,并且在GETTYPE语句上尝试了许多变种,我的TYPE总是为NULL。

例如,根据STACKOVERFLOW上的帖子,我试过:

typeToDeserialize = Type.GetType(String.Format("{0}, {1}", "System.Collections.Generic.List`1[[NorwayTax.Streaming+MyFile, NorwayTax]]", assemblyName));

但它不起作用:(

0 个答案:

没有答案