本文整理汇总了C#中ProtoBuf.Meta.TypeModel类的典型用法代码示例。如果您正苦于以下问题:C# TypeModel类的具体用法?C# TypeModel怎么用?C# TypeModel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TypeModel类属于ProtoBuf.Meta命名空间,在下文中一共展示了TypeModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NetObjectSerializer
public NetObjectSerializer(TypeModel model, Type type, int key, BclHelpers.NetObjectOptions options)
{
bool flag = (byte)(options & BclHelpers.NetObjectOptions.DynamicType) != 0;
this.key = ((!flag) ? key : -1);
this.type = ((!flag) ? type : model.MapType(typeof(object)));
this.options = options;
}
示例2: DeepClone_ClassFieldMaintainsReference
public void DeepClone_ClassFieldMaintainsReference(TypeModel model)
{
var item = new ItemClass() { Message = "Bob yo" };
var container = new ContainerItemClass()
{
Item1 = item,
Item2 = item
};
var clone = (ContainerItemClass)model.DeepClone(container);
Assert.IsTrue(object.ReferenceEquals(container.Item1, container.Item2));
Assert.IsTrue(object.ReferenceEquals(clone.Item1, clone.Item2));
Assert.AreEqual(container.Item1.Message, clone.Item1.Message);
Assert.AreEqual(container.Item2.Message, clone.Item2.Message);
container.Item1.Message = "New original";
clone.Item1.Message = "New clone";
Assert.AreEqual(container.Item1.Message, container.Item2.Message);
Assert.AreEqual(clone.Item1.Message, clone.Item2.Message);
container.Item2.Message = "New original 2";
clone.Item2.Message = "New clone 2";
Assert.AreEqual(container.Item1.Message, container.Item2.Message);
Assert.AreEqual(clone.Item1.Message, clone.Item2.Message);
}
示例3: Callback
public void Callback(object value, TypeModel.CallbackType callbackType)
{
if (callbacks != null) InvokeCallback(callbacks[callbackType], value);
IProtoTypeSerializer ser = (IProtoTypeSerializer)GetMoreSpecificSerializer(value);
if (ser != null) ser.Callback(value, callbackType);
}
示例4: DeepClone_DictionaryStringToObjectContainer
public void DeepClone_DictionaryStringToObjectContainer(TypeModel model)
{
var dictionary = new Dictionary<string, YellowPage>()
{
{ "A", new YellowPage() { Message = "Ahahaha" } },
{ "B", new YellowPage() { Message = "Blahblah" } },
{ "C", new YellowPage() { Message = "Caca" } },
};
var yellowPages = new YellowPages()
{
Collection = dictionary,
RefLink = dictionary
};
yellowPages.Collection.Add("D", new YellowPage() { Message = "Dannnnnnng!" });
var cloneYellowPages = (YellowPages)model.DeepClone(yellowPages);
Assert.AreEqual(yellowPages.Collection.Count, yellowPages.RefLink.Count);
foreach (var key in yellowPages.Collection.Keys)
{
Assert.AreEqual(yellowPages.Collection[key].Message, cloneYellowPages.Collection[key].Message);
}
cloneYellowPages.Collection.Add("E", new YellowPage() { Message = "Ello without H" });
Assert.AreEqual(cloneYellowPages.Collection.Count, cloneYellowPages.RefLink.Count);
}
示例5: GetKey
static int GetKey(TypeModel model, ref Type type, out bool isList)
{
if (model != null && type != null)
{
int key = model.GetKey(ref type);
if (key >= 0)
{
isList = false;
return key;
}
Type itemType = TypeModel.GetListItemType(model, type);
if (itemType != null)
{
key = model.GetKey(ref itemType);
if (key >= 0)
{
isList = true;
return key;
}
}
}
isList = false;
return -1;
}
示例6: Check
private void Check(TypeModel model, SerializationContext ctx, int magicNumber, string caption)
{
try
{
CanHazFactory orig = new CanHazFactory {Foo = 123, Bar = 456}, clone;
using(var ms = new MemoryStream())
{
model.Serialize(ms, orig, ctx);
ms.Position = 0;
clone = (CanHazFactory) model.Deserialize(ms, null, typeof(CanHazFactory), ctx);
}
Assert.AreNotSame(orig, clone);
Assert.AreEqual(123, orig.Foo, caption);
Assert.AreEqual(456, orig.Bar, caption);
Assert.AreEqual(0, orig.MagicNumber, caption);
Assert.AreEqual(123, clone.Foo, caption);
Assert.AreEqual(456, clone.Bar, caption);
Assert.AreEqual(magicNumber, clone.MagicNumber, caption);
} catch
{
Debug.WriteLine(caption);
throw;
}
}
示例7: DeepClone_ClassWithFields
public void DeepClone_ClassWithFields(TypeModel model)
{
var classWithFields =
new ClassWithFields(
12,
"lalala",
new Guid("e0947b41-437a-4bda-8a47-16451417f307"),
new ItemClass() { Message = "hello" },
new List<int>() { 23, 45 },
EnumTest.Item2,
0.00000m,
1.2345678m);
var clone = (ClassWithFields)model.DeepClone(classWithFields);
Assert.AreEqual(classWithFields.Guid, clone.Guid);
Assert.AreEqual(classWithFields.Integer, clone.Integer);
Assert.AreEqual(classWithFields.ItemClass.Message, clone.ItemClass.Message);
Assert.AreEqual(classWithFields.List.Count, clone.List.Count);
Assert.AreEqual(classWithFields.List[0], clone.List[0]);
Assert.AreEqual(classWithFields.List[1], clone.List[1]);
Assert.AreEqual(classWithFields.S, clone.S);
Assert.AreEqual(classWithFields.EnumTest, clone.EnumTest);
Assert.AreEqual(classWithFields._dec1, clone._dec1);
Assert.AreEqual(classWithFields._dec2, clone._dec2);
}
示例8: DeepClone_ClassWithFieldsRef_AsReference
public void DeepClone_ClassWithFieldsRef_AsReference(TypeModel model)
{
var classWithFields = new ClassWithFieldsRef();
var item = new ItemClass() { Message = "Hi there!" };
classWithFields._item1 = item;
classWithFields._item2 = item;
var list = new List<int> { 2, 4, 5 };
classWithFields._list1 = list;
classWithFields._list2 = list;
var clone = (ClassWithFieldsRef)model.DeepClone(classWithFields);
Assert.AreEqual(classWithFields._item1.Message, clone._item1.Message);
Assert.AreEqual(classWithFields._item2.Message, clone._item2.Message);
Assert.AreEqual(classWithFields._list1.Count, clone._list1.Count);
for (int i = 0; i < classWithFields._list1.Count; i++)
{
Assert.AreEqual(classWithFields._list1[i], clone._list1[i]);
}
Assert.AreEqual(classWithFields._list2.Count, clone._list2.Count);
for (int i = 0; i < classWithFields._list2.Count; i++)
{
Assert.AreEqual(classWithFields._list2[i], clone._list2[i]);
}
Assert.IsTrue(object.ReferenceEquals(classWithFields._item1, classWithFields._item2));
Assert.IsTrue(object.ReferenceEquals(classWithFields._list1, classWithFields._list2));
Assert.IsTrue(object.ReferenceEquals(clone._item1, clone._item2));
Assert.IsTrue(object.ReferenceEquals(clone._list1, clone._list2));
}
示例9: ArrayDecorator
public ArrayDecorator(TypeModel model, IProtoSerializer tail, int fieldNumber, bool writePacked, WireType packedWireType, Type arrayType, bool overwriteList, bool supportNull)
: base(tail)
{
this.itemType = arrayType.GetElementType();
Type arg_3E_0 = (!supportNull) ? (Helpers.GetUnderlyingType(this.itemType) ?? this.itemType) : this.itemType;
if ((writePacked || packedWireType != WireType.None) && fieldNumber <= 0)
{
throw new ArgumentOutOfRangeException("fieldNumber");
}
if (!ListDecorator.CanPack(packedWireType))
{
if (writePacked)
{
throw new InvalidOperationException("Only simple data-types can use packed encoding");
}
packedWireType = WireType.None;
}
this.fieldNumber = fieldNumber;
this.packedWireType = packedWireType;
if (writePacked)
{
this.options |= 1;
}
if (overwriteList)
{
this.options |= 2;
}
if (supportNull)
{
this.options |= 4;
}
this.arrayType = arrayType;
}
示例10: Check
void Check(TypeModel model)
{
var obj = Tuple.Create(
123, new[] { Tuple.Create(1, 2, 3, 4, 5, 6, 7, new List<Tuple<float, float>> { Tuple.Create(1F,2F) }), Tuple.Create(9, 10, 11, 12, 13, 14, 15, new List<Tuple<float, float>> { Tuple.Create(3F,4F) }) }, true);
var clone = (Tuple<int, Tuple<int, int, int, int, int, int, int, Tuple<List<Tuple<float, float>>>>[], bool>)model.DeepClone(obj);
Assert.AreEqual(123, clone.Item1);
Assert.AreEqual(2, clone.Item2.Length);
Assert.AreEqual(1, clone.Item2[0].Item1);
Assert.AreEqual(2, clone.Item2[0].Item2);
Assert.AreEqual(3, clone.Item2[0].Item3);
Assert.AreEqual(4, clone.Item2[0].Item4);
Assert.AreEqual(5, clone.Item2[0].Item5);
Assert.AreEqual(6, clone.Item2[0].Item6);
Assert.AreEqual(7, clone.Item2[0].Item7);
Assert.AreEqual(Tuple.Create(1F,2F), clone.Item2[0].Rest.Item1.Single());
Assert.AreEqual(9, clone.Item2[1].Item1);
Assert.AreEqual(10, clone.Item2[1].Item2);
Assert.AreEqual(11, clone.Item2[1].Item3);
Assert.AreEqual(12, clone.Item2[1].Item4);
Assert.AreEqual(13, clone.Item2[1].Item5);
Assert.AreEqual(14, clone.Item2[1].Item6);
Assert.AreEqual(15, clone.Item2[1].Item7);
Assert.AreEqual(Tuple.Create(3F, 4F), clone.Item2[1].Rest.Item1.Single());
Assert.AreEqual(true, clone.Item3);
}
示例11: ArrayDecorator
public ArrayDecorator(TypeModel model, IProtoSerializer tail, int fieldNumber, bool writePacked, WireType packedWireType, Type arrayType, bool overwriteList, bool supportNull)
: base(tail)
{
Helpers.DebugAssert(arrayType != null, "arrayType should be non-null");
Helpers.DebugAssert(arrayType.IsArray && arrayType.GetArrayRank() == 1, "should be single-dimension array; " + arrayType.FullName);
this.itemType = arrayType.GetElementType();
#if NO_GENERICS
Type underlyingItemType = itemType;
#else
Type underlyingItemType = supportNull ? itemType : (Helpers.GetUnderlyingType(itemType) ?? itemType);
#endif
Helpers.DebugAssert(underlyingItemType == Tail.ExpectedType, "invalid tail");
Helpers.DebugAssert(Tail.ExpectedType != model.MapType(typeof(byte)), "Should have used BlobSerializer");
if ((writePacked || packedWireType != WireType.None) && fieldNumber <= 0) throw new ArgumentOutOfRangeException("fieldNumber");
if (!ListDecorator.CanPack(packedWireType))
{
if (writePacked) throw new InvalidOperationException("Only simple data-types can use packed encoding");
packedWireType = WireType.None;
}
this.fieldNumber = fieldNumber;
this.packedWireType = packedWireType;
if (writePacked) options |= OPTIONS_WritePacked;
if (overwriteList) options |= OPTIONS_OverwriteList;
if (supportNull) options |= OPTIONS_SupportNull;
this.arrayType = arrayType;
}
示例12: ProtoOperationBehavior
/// <summary>
/// Create a new ProtoOperationBehavior instance
/// </summary>
public ProtoOperationBehavior(OperationDescription operation)
: base(operation)
{
#if !NO_RUNTIME
model = RuntimeTypeModel.Default;
#endif
}
示例13: DeepClone_FruitBag
public void DeepClone_FruitBag(TypeModel model)
{
var fruitBag = new FruitBag()
{
Apple = new Apple()
{
Age = 12,
StickerMessage = "13",
Taste = 14
},
Color = 15,
Pear = new Pear()
{
Age = 16,
StickerMessage = "17",
Hello = 18,
Taste = 19
}
};
var cloneFruitBag = (FruitBag)model.DeepClone(fruitBag);
Assert.AreEqual(fruitBag.Apple.Age, cloneFruitBag.Apple.Age);
Assert.AreEqual(fruitBag.Apple.StickerMessage, cloneFruitBag.Apple.StickerMessage);
Assert.AreEqual(fruitBag.Apple.Taste, cloneFruitBag.Apple.Taste);
Assert.AreEqual(fruitBag.Color, cloneFruitBag.Color);
Assert.AreEqual(fruitBag.Pear.Age, cloneFruitBag.Pear.Age);
Assert.AreEqual(fruitBag.Pear.StickerMessage, cloneFruitBag.Pear.StickerMessage);
Assert.AreEqual(fruitBag.Pear.Hello, cloneFruitBag.Pear.Hello);
Assert.AreEqual(fruitBag.Pear.Taste, cloneFruitBag.Pear.Taste);
}
示例14: DeepClone_DictionaryOfClassRef_AsReference
public void DeepClone_DictionaryOfClassRef_AsReference(TypeModel model)
{
var itemClass = new ItemClass() { Message = "hello"};
var dictionary = new DictionaryOfClass()
{
Collection = new Dictionary<int, ItemClass>
{
{ 1, itemClass },
{ 2, itemClass }
}
};
var clone = (DictionaryOfClass)model.DeepClone(dictionary);
Assert.AreEqual(dictionary.Collection.Count, clone.Collection.Count);
foreach (var key in dictionary.Collection.Keys)
{
Assert.AreEqual(dictionary.Collection[key].Message, clone.Collection[key].Message);
}
Assert.IsTrue(object.ReferenceEquals(dictionary.Collection[1], dictionary.Collection[2]), "Original reference failed");
Assert.IsTrue(object.ReferenceEquals(clone.Collection[1], clone.Collection[2]), "Clone reference not maintained");
}
示例15: Create
public static AttributeMap[] Create(TypeModel model, Assembly assembly)
{
#if FEAT_IKVM
const bool inherit = false;
System.Collections.Generic.IList<CustomAttributeData> all = assembly.__GetCustomAttributes(model.MapType(typeof(Attribute)), inherit);
AttributeMap[] result = new AttributeMap[all.Count];
int index = 0;
foreach (CustomAttributeData attrib in all)
{
result[index++] = new AttributeDataMap(attrib);
}
return result;
#else
#if WINRT || COREFX
Attribute[] all = System.Linq.Enumerable.ToArray(assembly.GetCustomAttributes());
#else
const bool inherit = false;
object[] all = assembly.GetCustomAttributes(inherit);
#endif
AttributeMap[] result = new AttributeMap[all.Length];
for(int i = 0 ; i < all.Length ; i++)
{
result[i] = new ReflectionAttributeMap((Attribute)all[i]);
}
return result;
#endif
}