本文整理汇总了C#中Serializer.GetSerializerByType方法的典型用法代码示例。如果您正苦于以下问题:C# Serializer.GetSerializerByType方法的具体用法?C# Serializer.GetSerializerByType怎么用?C# Serializer.GetSerializerByType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Serializer
的用法示例。
在下文中一共展示了Serializer.GetSerializerByType方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildSerializer
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var x = new ObjectSerializer(type);
typeMapping.TryAdd(type, x);
var elementType = GetEnumerableType(type);
var arrType = elementType.MakeArrayType();
var listModule = type.Assembly.GetType("Microsoft.FSharp.Collections.ListModule");
var ofArray = listModule.GetMethod("OfArray");
var ofArrayConcrete = ofArray.MakeGenericMethod(elementType);
var ofArrayCompiled = CodeGenerator.CompileToDelegate(ofArrayConcrete, arrType);
var toArray = listModule.GetMethod("ToArray");
var toArrayConcrete = toArray.MakeGenericMethod(elementType);
var toArrayCompiled = CodeGenerator.CompileToDelegate(toArrayConcrete, type);
ValueWriter writer = (stream, o, session) =>
{
var arr = toArrayCompiled(o);
var arrSerializer = serializer.GetSerializerByType(arrType);
arrSerializer.WriteValue(stream,arr,session);
};
ValueReader reader = (stream, session) =>
{
var arrSerializer = serializer.GetSerializerByType(arrType);
var items = (Array)arrSerializer.ReadValue(stream, session);
var res = ofArrayCompiled(items);
return res;
};
x.Initialize(reader, writer);
return x;
}
示例2: BuildSerializer
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var os = new ObjectSerializer(type);
typeMapping.TryAdd(type, os);
var methodInfoSerializer = serializer.GetSerializerByType(typeof(MethodInfo));
var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
ObjectReader reader = (stream, session) =>
{
var target = stream.ReadObject(session);
var method = (MethodInfo) stream.ReadObject(session);
var del = method.CreateDelegate(type, target);
return del;
};
ObjectWriter writer = (stream, value, session) =>
{
var d = (Delegate) value;
var method = d.GetMethodInfo();
stream.WriteObjectWithManifest(d.Target, session);
//less lookups, slightly faster
stream.WriteObject(method, type, methodInfoSerializer, preserveObjectReferences, session);
};
os.Initialize(reader, writer);
return os;
}
示例3: BuildSerializer
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var ser = new ObjectSerializer(type);
typeMapping.TryAdd(type, ser);
var elementSerializer = serializer.GetSerializerByType(typeof (DictionaryEntry));
ValueReader reader = (stream, session) =>
{
var count = stream.ReadInt32(session);
var entries = new DictionaryEntry[count];
for (var i = 0; i < count; i++)
{
var entry = (DictionaryEntry) stream.ReadObject(session);
entries[i] = entry;
}
return null;
};
ValueWriter writer = (stream, obj, session) =>
{
var dict = obj as IDictionary;
// ReSharper disable once PossibleNullReferenceException
stream.WriteInt32(dict.Count);
foreach (var item in dict)
{
stream.WriteObject(item, typeof (DictionaryEntry), elementSerializer,
serializer.Options.PreserveObjectReferences, session);
// elementSerializer.WriteValue(stream,item,session);
}
};
ser.Initialize(reader, writer);
return ser;
}
示例4: BuildSerializer
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var x = new ObjectSerializer(type);
typeMapping.TryAdd(type, x);
var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
var elementType = GetEnumerableType(type) ?? typeof (object);
var elementSerializer = serializer.GetSerializerByType(elementType);
ValueWriter writer = (stream, o, session) =>
{
var enumerable = o as ICollection;
// ReSharper disable once PossibleNullReferenceException
stream.WriteInt32(enumerable.Count);
foreach (var value in enumerable)
{
stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
}
};
ValueReader reader = (stream, session) =>
{
var count = stream.ReadInt32(session);
var items = Array.CreateInstance(elementType, count);
for (var i = 0; i < count; i++)
{
var value = stream.ReadObject(session);
items.SetValue(value, i);
}
//HACK: this needs to be fixed, codegenerated or whatever
var instance = Activator.CreateInstance(type);
var addRange = type.GetMethod("AddRange");
if (addRange != null)
{
addRange.Invoke(instance, new object[] {items});
return instance;
}
var add = type.GetMethod("Add");
if (add != null)
{
for (var i = 0; i < items.Length; i++)
{
add.Invoke(instance, new[] {items.GetValue(i)});
}
}
return instance;
};
x.Initialize(reader, writer);
return x;
}
示例5: BuildSerializer
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
var ser = new ObjectSerializer(type);
typeMapping.TryAdd(type, ser);
var elementSerializer = serializer.GetSerializerByType(typeof (DictionaryEntry));
ObjectReader reader = (stream, session) =>
{
throw new NotSupportedException("Generic IDictionary<TKey,TValue> are not yet supported");
#pragma warning disable CS0162 // Unreachable code detected
var instance = Activator.CreateInstance(type);
#pragma warning restore CS0162 // Unreachable code detected
if (preserveObjectReferences)
{
session.TrackDeserializedObject(instance);
}
var count = stream.ReadInt32(session);
var entries = new DictionaryEntry[count];
for (var i = 0; i < count; i++)
{
var entry = (DictionaryEntry) stream.ReadObject(session);
entries[i] = entry;
}
//TODO: populate dictionary
return instance;
};
ObjectWriter writer = (stream, obj, session) =>
{
if (preserveObjectReferences)
{
session.TrackSerializedObject(obj);
}
var dict = obj as IDictionary;
// ReSharper disable once PossibleNullReferenceException
Int32Serializer.WriteValueImpl(stream,dict.Count,session);
foreach (var item in dict)
{
stream.WriteObject(item, typeof (DictionaryEntry), elementSerializer,
serializer.Options.PreserveObjectReferences, session);
// elementSerializer.WriteValue(stream,item,session);
}
};
ser.Initialize(reader, writer);
return ser;
}
示例6: BuildSerializer
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var ser = new ObjectSerializer(type);
typeMapping.TryAdd(type, ser);
var elementSerializer = serializer.GetSerializerByType(typeof (DictionaryEntry));
var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
ObjectReader reader = (stream, session) =>
{
var count = stream.ReadInt32(session);
var instance = (IDictionary) Activator.CreateInstance(type, count);
if (preserveObjectReferences)
{
session.TrackDeserializedObject(instance);
}
for (var i = 0; i < count; i++)
{
var entry = (DictionaryEntry) stream.ReadObject(session);
instance.Add(entry.Key, entry.Value);
}
return instance;
};
ObjectWriter writer = (stream, obj, session) =>
{
if (preserveObjectReferences)
{
session.TrackSerializedObject(obj);
}
var dict = obj as IDictionary;
// ReSharper disable once PossibleNullReferenceException
Int32Serializer.WriteValueImpl(stream,dict.Count,session);
foreach (DictionaryEntry item in dict)
{
stream.WriteObject(item, typeof (DictionaryEntry), elementSerializer,
serializer.Options.PreserveObjectReferences, session);
// elementSerializer.WriteValue(stream,item,session);
}
};
ser.Initialize(reader, writer);
return ser;
}
示例7: BuildSerializer
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var serializableSerializer = new ObjectSerializer(type);
typeMapping.TryAdd(type, serializableSerializer);
ValueReader reader = (stream, session) =>
{
var dict = stream.ReadObject(session) as Dictionary<string, object>;
var info = new SerializationInfo(type, new FormatterConverter());
// ReSharper disable once PossibleNullReferenceException
foreach (var item in dict)
{
info.AddValue(item.Key, item.Value);
}
// protected Dictionary(SerializationInfo info, StreamingContext context);
var flags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Default;
var ctor = type.GetConstructor(flags, null,
new[] {typeof (SerializationInfo), typeof (StreamingContext)}, null);
var instance = ctor.Invoke(new object[] {info, new StreamingContext()});
var deserializationCallback = instance as IDeserializationCallback;
deserializationCallback?.OnDeserialization(this);
return instance;
};
ValueWriter writer = (stream, o, session) =>
{
var info = new SerializationInfo(type, new FormatterConverter());
var serializable = o as ISerializable;
// ReSharper disable once PossibleNullReferenceException
serializable.GetObjectData(info, new StreamingContext());
var dict = new Dictionary<string, object>();
foreach (var item in info)
{
dict.Add(item.Name, item.Value);
}
var dictSerializer = serializer.GetSerializerByType(typeof (Dictionary<string, object>));
stream.WriteObject(dict, typeof (Dictionary<string, object>), dictSerializer,
serializer.Options.PreserveObjectReferences, session);
};
serializableSerializer.Initialize(reader, writer);
return serializableSerializer;
}
示例8: BuildSerializer
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var x = new ObjectSerializer(type);
typeMapping.TryAdd(type, x);
var keyType = GetKeyType(type);
var valueType = GetValyeType(type);
var tupleType = typeof(Tuple<,>).MakeGenericType(keyType, valueType);
var arrType = tupleType.MakeArrayType();
var mapModule = type.GetTypeInfo().Assembly.GetType("Microsoft.FSharp.Collections.MapModule");
var ofArray = mapModule.GetTypeInfo().GetMethod("OfArray");
var ofArrayConcrete = ofArray.MakeGenericMethod(keyType,valueType);
var ofArrayCompiled = CompileToDelegate(ofArrayConcrete, arrType);
var toArray = mapModule.GetTypeInfo().GetMethod("ToArray");
var toArrayConcrete = toArray.MakeGenericMethod(keyType,valueType);
var toArrayCompiled = CompileToDelegate(toArrayConcrete, type);
var arrSerializer = serializer.GetSerializerByType(arrType);
var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
ObjectWriter writer = (stream, o, session) =>
{
var arr = toArrayCompiled(o);
arrSerializer.WriteValue(stream, arr, session);
if (preserveObjectReferences)
{
session.TrackSerializedObject(o);
}
};
ObjectReader reader = (stream, session) =>
{
var arr = arrSerializer.ReadValue(stream, session);
var res = ofArrayCompiled(arr);
return res;
};
x.Initialize(reader, writer);
return x;
}
示例9: GenerateFieldInfoSerializer
private static Action<Stream, object, SerializerSession> GenerateFieldInfoSerializer(Serializer serializer, FieldInfo field)
{
if (serializer == null)
throw new ArgumentNullException(nameof(serializer));
if (field == null)
throw new ArgumentNullException(nameof(field));
//get the serializer for the type of the field
var valueSerializer = serializer.GetSerializerByType(field.FieldType);
//runtime generate a delegate that reads the content of the given field
var getFieldValue = GenerateFieldInfoReader(field);
//if the type is one of our special primitives, ignore manifest as the content will always only be of this type
if (!serializer.Options.VersionTolerance && Serializer.IsPrimitiveType(field.FieldType))
{
//primitive types does not need to write any manifest, if the field type is known
//nor can they be null (StringSerializer has it's own null handling)
Action<Stream, object, SerializerSession> fieldWriter = (stream, o, session) =>
{
var value = getFieldValue(o);
valueSerializer.WriteValue(stream, value, session);
};
return fieldWriter;
}
else
{
var valueType = field.FieldType;
if (field.FieldType.IsGenericType && field.FieldType.GetGenericTypeDefinition() == typeof (Nullable<>))
{
var nullableType = field.FieldType.GetGenericArguments()[0];
valueSerializer = serializer.GetSerializerByType(nullableType);
valueType = nullableType;
}
var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
Action<Stream, object, SerializerSession> fieldWriter = (stream, o, session) =>
{
var value = getFieldValue(o);
stream.WriteObject(value, valueType, valueSerializer, preserveObjectReferences, session);
};
return fieldWriter;
}
}
示例10: GenerateFieldInfoDeserializer
private static Action<Stream, object, SerializerSession> GenerateFieldInfoDeserializer(Serializer serializer,
Type type, FieldInfo field)
{
if (serializer == null)
throw new ArgumentNullException(nameof(serializer));
if (type == null)
throw new ArgumentNullException(nameof(type));
if (field == null)
throw new ArgumentNullException(nameof(field));
var s = serializer.GetSerializerByType(field.FieldType);
Action<object, object> setter;
if (field.IsInitOnly)
{
setter = field.SetValue;
}
else
{
var targetExp = Parameter(typeof (object), "target");
var valueExp = Parameter(typeof (object), "value");
Expression castTartgetExp = field.DeclaringType.IsValueType
? Unbox(targetExp, type)
: Convert(targetExp, type);
Expression castValueExp = Convert(valueExp, field.FieldType);
var fieldExp = Field(castTartgetExp, field);
var assignExp = Assign(fieldExp, castValueExp);
setter = Lambda<Action<object, object>>(assignExp, targetExp, valueExp).Compile();
}
if (!serializer.Options.VersionTolerance && Serializer.IsPrimitiveType(field.FieldType))
{
//Only optimize if property names are not included.
//if they are included, we need to be able to skip past unknown property data
//e.g. if sender have added a new property that the receiveing end does not yet know about
//which we cannot do w/o a manifest
Action<Stream, object, SerializerSession> fieldReader = (stream, o, session) =>
{
var value = s.ReadValue(stream, session);
setter(o, value);
};
return fieldReader;
}
else
{
Action<Stream, object, SerializerSession> fieldReader = (stream, o, session) =>
{
var value = stream.ReadObject(session);
setter(o, value);
};
return fieldReader;
}
}
示例11: BuildSerializer
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var x = new ObjectSerializer(type);
typeMapping.TryAdd(type, x);
var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
var elementType = GetEnumerableType(type) ?? typeof (object);
var elementSerializer = serializer.GetSerializerByType(elementType);
ValueWriter writer = (stream, o, session) =>
{
var enumerable = o as ICollection;
if (enumerable == null)
{
// object can be IEnumerable but not ICollection i.e. ImmutableQueue
var e = (IEnumerable) o;
var list = new ArrayList();
foreach (var element in e)
list.Add(element);
enumerable = list;
}
stream.WriteInt32(enumerable.Count);
foreach (var value in enumerable)
{
stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
}
};
ValueReader reader = (stream, session) =>
{
var count = stream.ReadInt32(session);
var items = Array.CreateInstance(elementType, count);
for (var i = 0; i < count; i++)
{
var value = stream.ReadObject(session);
items.SetValue(value, i);
}
//HACK: this needs to be fixed, codegenerated or whatever
if (type.Namespace != null && type.Namespace.Equals(ImmutableCollectionsNamespace))
{
var typeName = type.Name;
var genericSufixIdx = typeName.IndexOf('`');
typeName = genericSufixIdx != -1 ? typeName.Substring(0, genericSufixIdx) : typeName;
var creatorType =
Type.GetType(
ImmutableCollectionsNamespace + "." + typeName + ", " + ImmutableCollectionsAssembly, true);
var genericTypes = elementType.IsGenericType
? elementType.GetGenericArguments()
: new[] {elementType};
var createRange = creatorType.GetMethods(BindingFlags.Public | BindingFlags.Static)
.First(methodInfo => methodInfo.Name == "CreateRange" && methodInfo.GetParameters().Length == 1)
.MakeGenericMethod(genericTypes);
var instance = createRange.Invoke(null, new object[] {items});
return instance;
}
else
{
var instance = Activator.CreateInstance(type);
var addRange = type.GetMethod("AddRange");
if (addRange != null)
{
addRange.Invoke(instance, new object[] {items});
return instance;
}
var add = type.GetMethod("Add");
if (add != null)
{
for (var i = 0; i < items.Length; i++)
{
add.Invoke(instance, new[] {items.GetValue(i)});
}
}
return instance;
}
};
x.Initialize(reader, writer);
return x;
}
示例12: GenerateFieldDeserializer
private static Action<Stream, object, SerializerSession> GenerateFieldDeserializer(Serializer serializer,
Type type, FieldInfo field)
{
var s = serializer.GetSerializerByType(field.FieldType);
Action<object, object> setter;
if (field.IsInitOnly)
{
setter = field.SetValue;
}
else
{
ParameterExpression targetExp = Expression.Parameter(typeof(object), "target");
ParameterExpression valueExp = Expression.Parameter(typeof(object), "value");
Expression castTartgetExp = field.DeclaringType.IsValueType
? Expression.Unbox(targetExp, type)
: Expression.Convert(targetExp, type);
Expression castValueExp = Expression.Convert(valueExp, field.FieldType);
MemberExpression fieldExp = Expression.Field(castTartgetExp, field);
BinaryExpression assignExp = Expression.Assign(fieldExp, castValueExp);
setter = Expression.Lambda<Action<object, object>>(assignExp, targetExp, valueExp).Compile();
}
if (!serializer.Options.VersionTolerance && Serializer.IsPrimitiveType(field.FieldType))
{
//Only optimize if property names are not included.
//if they are included, we need to be able to skip past unknown property data
//e.g. if sender have added a new property that the receiveing end does not yet know about
//which we cannot do w/o a manifest
Action<Stream, object, SerializerSession> fieldReader = (stream, o, session) =>
{
var value = s.ReadValue(stream, session);
setter(o, value);
//var x = field.GetValue(o);
//if (value != null && !value.Equals(x))
//{
//}
// field.SetValue(o, value);
};
return fieldReader;
}
else
{
Action<Stream, object, SerializerSession> fieldReader = (stream, o, session) =>
{
var value = stream.ReadObject(session);
// field.SetValue(o, value);
setter(o, value);
//var x = field.GetValue(o);
//if (value != null && !value.Equals(x))
//{
//}
};
return fieldReader;
}
}
示例13: BuildSerializer
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var x = new ObjectSerializer(type);
typeMapping.TryAdd(type, x);
var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
var elementType = GetEnumerableType(type) ?? typeof (object);
var elementSerializer = serializer.GetSerializerByType(elementType);
var typeName = type.Name;
var genericSufixIdx = typeName.IndexOf('`');
typeName = genericSufixIdx != -1 ? typeName.Substring(0, genericSufixIdx) : typeName;
var creatorType =
Type.GetType(
ImmutableCollectionsNamespace + "." + typeName + ", " + ImmutableCollectionsAssembly, true);
var genericTypes = elementType.GetTypeInfo().IsGenericType
? elementType.GetTypeInfo().GetGenericArguments()
: new[] { elementType };
var createRange = creatorType.GetTypeInfo().GetMethods(BindingFlags.Public | BindingFlags.Static)
.First(methodInfo => methodInfo.Name == "CreateRange" && methodInfo.GetParameters().Length == 1)
.MakeGenericMethod(genericTypes);
ObjectWriter writer = (stream, o, session) =>
{
var enumerable = o as ICollection;
if (enumerable == null)
{
// object can be IEnumerable but not ICollection i.e. ImmutableQueue
var e = (IEnumerable) o;
var list = e.Cast<object>().ToList();//
enumerable = list;
}
Int32Serializer.WriteValueImpl(stream,enumerable.Count,session);
foreach (var value in enumerable)
{
stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
}
if (preserveObjectReferences)
{
session.TrackSerializedObject(o);
}
};
ObjectReader reader = (stream, session) =>
{
var count = stream.ReadInt32(session);
var items = Array.CreateInstance(elementType, count);
for (var i = 0; i < count; i++)
{
var value = stream.ReadObject(session);
items.SetValue(value, i);
}
var instance = createRange.Invoke(null, new object[] {items});
if (preserveObjectReferences)
{
session.TrackDeserializedObject(instance);
}
return instance;
};
x.Initialize(reader, writer);
return x;
}