日期:2014-05-18  浏览次数:20490 次

一个Remoting中的实体序列化问题
各位:List <MyClass>     类型只要我继承它,并在子类中标识可序列化,那么就可以在remoting中传输,如下,
[Serializable]
        public   class   ItemList   :   List <MyItem>
        {         }
但是如果是Dictionary <String,   MyItem> 我即使是继承了可序列化,还是不能在netremoting中传输,如下
[Serializable]
        public   class   ItemList   :   Dictionary <String,   Item>
        {         }
传输的时候提示错误:Soap   序列化程序不支持序列化一般类型:   System.Collections.Generic.GenericEqualityComparer`1[System.String]。
怎么解决这个问题

------解决方案--------------------
Serializable序列化值类型
而MarshalByRefObject序列化引用类型
------解决方案--------------------
友情帮顶
------解决方案--------------------
远程处理对于序列化和反序列化有特殊的要求,这些要求在msdn中的“可远程处理的对象”和“不可远程处理的对象”中有着详细的描述。
以下是字典集合使用的序列化:
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) {
if (info==null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.info);
}
info.AddValue(VersionName, version);
info.AddValue(ComparerName, comparer, typeof(IEqualityComparer <TKey> ));
info.AddValue(HashSizeName, buckets == null ? 0 : buckets.Length); //This is the length of the bucket array.
if( buckets != null) {
KeyValuePair <TKey, TValue> [] array = new KeyValuePair <TKey, TValue> [Count];
CopyTo(array, 0);
info.AddValue(KeyValuePairsName, array, typeof(KeyValuePair <TKey, TValue> []));
}
}

System.Collections.Generic.GenericEqualityComparer是一个不可远程处理的对象,当然导致Dictionary <TK,TV> 也是不可远程处理的对象,你必须提供自己扩展实现,以便Dictionary <TK,TV> 可以远程处理。

以下是比较器的定义的源代码:
[Serializable()]
[TypeDependencyAttribute( "System.Collections.Generic.GenericEqualityComparer`1 ")]
public abstract class EqualityComparer <T> : IEqualityComparer, IEqualityComparer <T>
{
static EqualityComparer <T> defaultComparer;

public static EqualityComparer <T> Default {
get {
EqualityComparer <T> comparer = defaultComparer;
if (comparer == null) {
comparer = CreateComparer();
defaultComparer = comparer;
}
return comparer;
}
}

private static EqualityComparer <T> CreateComparer() {
Type t = typeof(T);
// Specialize type byte for performance reasons
if (t == typeof(byte)) {
return (EqualityComparer <T> )(object)(new ByteEqualityComparer());
}
// If T implements IEquatable <T> return a GenericEqualityComparer <T>
if (typeof(IEquatable <T> ).IsAssignableFrom(t)) {
//return (EqualityComparer <T> )Activator.CreateInstance(typeof(GenericEqualityComparer <> ).MakeGenericType(t));
return (EqualityComparer <T> )(typeof(GenericEqualityComparer <int> ).TypeHandle.CreateInstanceForAnotherGenericParameter(t));
}
// If T is a Nullable <U