本文整理汇总了C#中SerializationContext.GetSerializer方法的典型用法代码示例。如果您正苦于以下问题:C# SerializationContext.GetSerializer方法的具体用法?C# SerializationContext.GetSerializer怎么用?C# SerializationContext.GetSerializer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SerializationContext
的用法示例。
在下文中一共展示了SerializationContext.GetSerializer方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetConcreteSerializer
public static void GetConcreteSerializer(
SerializationContext context,
PolymorphismSchema schema,
Type abstractType,
Type targetType,
Type exampleType,
out ICollectionInstanceFactory factory,
out MessagePackSerializer serializer
)
{
if ( abstractType == targetType )
{
throw SerializationExceptions.NewNotSupportedBecauseCannotInstanciateAbstractType( abstractType );
}
serializer = context.GetSerializer( targetType, schema );
factory = serializer as ICollectionInstanceFactory;
if ( factory == null && (serializer as IPolymorphicDeserializer) == null )
{
throw SerializationExceptions.NewIncompatibleCollectionSerializer( abstractType, serializer.GetType(), exampleType );
}
}
示例2: ObjectReflectionMessagePackSerializer
protected ObjectReflectionMessagePackSerializer(Type type, SerializationContext context, SerializingMember[] members)
: base(type, (context ?? SerializationContext.Default).CompatibilityOptions.PackerCompatibilityOptions)
{
if (type.GetIsAbstract() || type.GetIsInterface())
{
throw SerializationExceptions.NewNotSupportedBecauseCannotInstanciateAbstractType(type);
}
this._createInstance = () => Activator.CreateInstance(type);
//Expression.Lambda<Func<T>>(
// typeof(T).GetIsValueType()
// ? Expression.Default(typeof(T)) as Expression
// : Expression.New(typeof(T).GetConstructor(ReflectionAbstractions.EmptyTypes))
// ).Compile();
var isPackable = typeof(IPackable).IsAssignableFrom(type);
var isUnpackable = typeof(IUnpackable).IsAssignableFrom(type);
if (isPackable && isUnpackable)
{
this._memberSerializers = null;
this._indexMap = null;
this._isCollection = null;
this._nilImplications = null;
this._memberNames = null;
}
else
{
this._memberSerializers =
members.Select(
m => m.Member == null ? NullSerializer.Instance : context.GetSerializer(m.Member.GetMemberValueType())).ToArray
(
);
this._indexMap =
members
.Select((m, i) => new KeyValuePair<SerializingMember, int>(m, i))
.Where(kv => kv.Key.Member != null)
.ToDictionary(kv => kv.Key.Contract.Name, kv => kv.Value);
this._isCollection =
members.Select(
m => m.Member == null ? CollectionTraits.NotCollection : m.Member.GetMemberValueType().GetCollectionTraits()).
Select(t => t.CollectionType != CollectionKind.NotCollection).ToArray();
// NilImplication validity check
foreach (var member in members)
{
switch (member.Contract.NilImplication)
{
case NilImplication.Null:
{
if (member.Member.GetMemberValueType().GetIsValueType() &&
Nullable.GetUnderlyingType(member.Member.GetMemberValueType()) == null)
{
throw SerializationExceptions.NewValueTypeCannotBeNull(
member.Contract.Name, member.Member.GetMemberValueType(), member.Member.DeclaringType
);
}
if (!member.Member.CanSetValue())
{
throw SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNull(member.Contract.Name);
}
break;
}
}
}
this._nilImplications = members.Select(m => m.Contract.NilImplication).ToArray();
this._memberNames = members.Select(m => m.Contract.Name).ToArray();
}
if (isPackable)
{
this._packToMessage = (target, packer, packingOptions) =>
{
((IPackable)target).PackToMessage(packer, packingOptions);
//typeof(T).GetInterfaceMap(typeof(IPackable)).TargetMethods.Single().Invoke(target, new object[] { packer, packingOptions });
};
this._memberGetters = null;
}
else
{
this._packToMessage = null;
this._memberGetters =
members.Select<SerializingMember,Func<object,object>>(
m => m.Member == null ? (target => null)
: CreateMemberGetter(m)).ToArray();
}
if (isUnpackable)
{
this._unpackFromMessage = delegate(ref object target, Unpacker value)
{
((IUnpackable)target).UnpackFromMessage(value);
};
this._memberSetters = null;
//.........这里部分代码省略.........
示例3: SequenceReflectionMessagePackSerializer
protected SequenceReflectionMessagePackSerializer(Type type, SerializationContext context, CollectionTraits traits)
: base(type, (context ?? SerializationContext.Default).CompatibilityOptions.PackerCompatibilityOptions)
{
Contract.Assert(type.IsArray || typeof(IEnumerable).IsAssignableFrom(type), type + " is not array nor IEnumerable");
this._traits = traits;
this._elementSerializer = context.GetSerializer(traits.ElementType);
this._getCount = ReflectionSerializerLogics.CreateGetCount(type, traits);
//var packerParameter = Expression.Parameter(typeof(Packer), "packer");
//var objectTreeParameter = Expression.Parameter(typeof(T), "objectTree");
//var elementSerializerParameter = Expression.Parameter(typeof(IMessagePackSerializer), "elementSerializer");
this._packToCore = (Packer packer, object objectTree, IMessagePackSerializer elementSerializer) =>
{
var length = this._getCount(objectTree);
packer.PackArrayHeader(length);
foreach (var item in (IEnumerable)objectTree)
{
elementSerializer.PackTo(packer, item);
}
};
/*
* for ( int i = 0; i < count; i++ )
* {
* if ( !unpacker.Read() )
* {
* throw SerializationExceptions.NewMissingItem( i );
* }
*
* T item;
* if ( !unpacker.IsArrayHeader && !unpacker.IsMapHeader )
* {
* item = this.ElementSerializer.UnpackFrom( unpacker );
* }
* else
* {
* using ( Unpacker subtreeUnpacker = unpacker.ReadSubtree() )
* {
* item = this.ElementSerializer.UnpackFrom( subtreeUnpacker );
* }
* }
*
* instance[ i ] = item; -- OR -- instance.Add( item );
* }
*/
// FIXME: use UnpackHelper
if (type.IsArray)
{
var arrayUnpackerMethod = _UnpackHelpers.UnpackArrayTo_1.MakeGenericMethod(traits.ElementType);
this._unpackToCore = (Unpacker unpacker, object instance, IMessagePackSerializer elementSerializer) =>
{
arrayUnpackerMethod.Invoke(null, new object[] { unpacker, elementSerializer, instance });
};
}
else
{
this._unpackToCore = (Unpacker unpacker, object instance, IMessagePackSerializer elementSerializer) =>
{
var count = UnpackHelpers.GetItemsCount(unpacker);
for (int i = 0; i < count; i++)
{
if (!unpacker.Read())
{
throw SerializationExceptions.NewMissingItem(i);
}
object item;
if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader)
{
item = elementSerializer.UnpackFrom(unpacker);
}
else
{
using (Unpacker subtreeUnpacker = unpacker.ReadSubtree())
{
item = elementSerializer.UnpackFrom(subtreeUnpacker);
}
}
traits.AddMethod.Invoke(instance, new object[] { item });
}
};
}
}
示例4: MapReflectionMessagePackSerializer
public MapReflectionMessagePackSerializer(Type type, SerializationContext context, CollectionTraits traits)
: base(type, (context ?? SerializationContext.Default).CompatibilityOptions.PackerCompatibilityOptions)
{
Contract.Assert(typeof(IEnumerable).IsAssignableFrom(type), type + " is IEnumerable");
Contract.Assert(traits.ElementType == typeof(DictionaryEntry) || (traits.ElementType.GetIsGenericType() && traits.ElementType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>)), "Element type " + traits.ElementType + " is not KeyValuePair<TKey,TValue>.");
this._traits = traits;
this._keySerializer = traits.ElementType.GetIsGenericType() ? context.GetSerializer(traits.ElementType.GetGenericArguments()[0]) : context.GetSerializer(typeof(MessagePackObject));
this._valueSerializer = traits.ElementType.GetIsGenericType() ? context.GetSerializer(traits.ElementType.GetGenericArguments()[1]) : context.GetSerializer(typeof(MessagePackObject));
this._getCount = ReflectionSerializerLogics.CreateGetCount(type, traits);
var constructor = ReflectionSerializerLogics.GetCollectionConstructor(context, type);
if (constructor == null)
{
this._createInstance = () => { throw SerializationExceptions.NewTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity(type); };
this._createInstanceWithCapacity = null;
}
else if (constructor.GetParameters().Length == 1)
{
this._createInstance = null;
this._createInstanceWithCapacity = length => constructor.Invoke(new object[] { length });
}
else
{
this._createInstanceWithCapacity = null;
this._createInstance = () => constructor.Invoke(new object[0]);
}
var keyType = traits.ElementType.GetIsGenericType() ? traits.ElementType.GetGenericArguments()[0] : typeof(MessagePackObject);
var valueType = traits.ElementType.GetIsGenericType() ? traits.ElementType.GetGenericArguments()[1] : typeof(MessagePackObject);
var keyProperty = traits.ElementType.GetProperty("Key");
var valueProperty = traits.ElementType.GetProperty("Value");
this._packToCore = (Packer packer, object objectTree, IMessagePackSerializer keySerializer, IMessagePackSerializer valueSerializer) =>
{
packer.PackMapHeader(this._getCount(objectTree));
foreach (var kvp in (IEnumerable)objectTree)
{
keySerializer.PackTo(packer, keyProperty.GetValue(kvp, new object[0]));
valueSerializer.PackTo(packer, valueProperty.GetValue(kvp, new object[0]));
}
};
if (traits.ElementType.GetIsGenericType())
{
/*
* UnpackHelpers.UnpackMapTo<TKey,TValue>( unpacker, keySerializer, valueSerializer, instance );
*/
var unpackMapToMethod = Metadata._UnpackHelpers.UnpackMapTo_2; //.MakeGenericMethod(keyType, valueType);
this._unpackToCore = (Unpacker unpacker, object objectTree, IMessagePackSerializer keySerializer, IMessagePackSerializer valueSerializer) =>
{
unpackMapToMethod.Invoke(null, new object[] { unpacker, keySerializer, valueSerializer, objectTree });
};
}
else
{
/*
* UnpackHelpers.UnpackNonGenericMapTo( unpacker, instance );
*/
this._unpackToCore = (Unpacker unpacker, object objectTree, IMessagePackSerializer keySerializer, IMessagePackSerializer valueSerializer) => UnpackHelpers.UnpackMapTo(unpacker, (IDictionary)objectTree);
}
}