各位: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]
public class ItemList : List<MyItem>
{ }
但是如果是Dictionary<String, MyItem>我即使是继承了可序列化,还是不能在netremoting中传输,如下
[Serializable]
public class ItemList : Dictionary<String, Item>
{ }
传输的时候提示错误:Soap 序列化程序不支持序列化一般类型: System.Collections.Generic.GenericEqualityComparer`1[System.String]。
怎么解决这个问题
而MarshalByRefObject序列化引用类型
而MarshalByRefObject序列化引用类型】楼上的兄台,这句话颇有问题。 呵呵
以下是字典集合使用的序列化:
[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> where U implements IEquatable<U> return a NullableEqualityComparer<U>
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) {
Type u = t.GetGenericArguments()[0];
if (typeof(IEquatable<>).MakeGenericType(u).IsAssignableFrom(u)) {
//return (EqualityComparer<T>)Activator.CreateInstance(typeof(NullableEqualityComparer<>).MakeGenericType(u));
return (EqualityComparer<T>)(typeof(NullableEqualityComparer<int>).TypeHandle.CreateInstanceForAnotherGenericParameter(u));
}
}
// Otherwise return an ObjectEqualityComparer<T>
return new ObjectEqualityComparer<T>();
} public abstract bool Equals(T x, T y);
public abstract int GetHashCode(T obj); internal virtual int IndexOf(T[] array, T value, int startIndex, int count) {
int endIndex = startIndex + count;
for (int i = startIndex; i < endIndex; i++) {
if (Equals(array[i], value)) return i;
}
return -1;
} internal virtual int LastIndexOf(T[] array, T value, int startIndex, int count) {
int endIndex = startIndex - count + 1;
for (int i = startIndex; i >= endIndex; i--) {
if (Equals(array[i], value)) return i;
}
return -1;
} int IEqualityComparer.GetHashCode(object obj) {
if (obj == null) return 0;
if (obj is T) return GetHashCode((T)obj);
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArgumentForComparison);
return 0;
} bool IEqualityComparer.Equals(object x, object y) {
if (x == y) return true;
if (x == null || y == null) return false;
if ((x is T) && (y is T)) return Equals((T)x, (T)y);
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArgumentForComparison);
return false;
}
} // The methods in this class look identical to the inherited methods, but the calls
// to Equal bind to IEquatable<T>.Equals(T) instead of Object.Equals(Object)
[Serializable()]
internal class GenericEqualityComparer<T>: EqualityComparer<T> where T: IEquatable<T>
{
public override bool Equals(T x, T y) {
if (x != null) {
if (y != null) return x.Equals(y);
return false;
}
if (y != null) return false;
return true;
} public override int GetHashCode(T obj) {
if (obj == null) return 0;
return obj.GetHashCode();
} internal override int IndexOf(T[] array, T value, int startIndex, int count) {
int endIndex = startIndex + count;
if (value == null) {
for (int i = startIndex; i < endIndex; i++) {
if (array[i] == null) return i;
}
}
else {
for (int i = startIndex; i < endIndex; i++) {
if (array[i] != null && array[i].Equals(value)) return i;
}
}
return -1;
} internal override int LastIndexOf(T[] array, T value, int startIndex, int count) {
int endIndex = startIndex - count + 1;
if (value == null) {
for (int i = startIndex; i >= endIndex; i--) {
if (array[i] == null) return i;
}
}
else {
for (int i = startIndex; i >= endIndex; i--) {
if (array[i] != null && array[i].Equals(value)) return i;
}
}
return -1;
} // Equals method for the comparer itself.
public override bool Equals(Object obj){
GenericEqualityComparer<T> comparer = obj as GenericEqualityComparer<T>;
return comparer != null;
} public override int GetHashCode() {
return this.GetType().Name.GetHashCode();
}
}
<serverProviders>
<formatter ref="binary" />
</serverProviders>