本文整理汇总了C#中ProtoBuf.Meta.BasicList类的典型用法代码示例。如果您正苦于以下问题:C# BasicList类的具体用法?C# BasicList怎么用?C# BasicList使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BasicList类属于ProtoBuf.Meta命名空间,在下文中一共展示了BasicList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddSubType
/// <summary>
/// Adds a known sub-type to the inheritance model
/// </summary>
public MetaType AddSubType(int fieldNumber, Type derivedType)
{
if (derivedType == null) throw new ArgumentNullException("derivedType");
if (fieldNumber < 1) throw new ArgumentOutOfRangeException("fieldNumber");
if (!(type.IsClass || type.IsInterface) || type.IsSealed) {
throw new InvalidOperationException("Sub-types can only be added to non-sealed classes");
}
if (typeof(IEnumerable).IsAssignableFrom(type))
{
throw new ArgumentException("Repeated data (a list, collection, etc) has inbuilt behaviour and cannot be subclassed");
}
if (typeof(IEnumerable).IsAssignableFrom(derivedType))
{
throw new ArgumentException("Repeated data (a list, collection, etc) has inbuilt behaviour and cannot be used as a subclass");
}
ThrowIfFrozen();
MetaType derivedMeta = model[derivedType];
derivedMeta.ThrowIfFrozen();
SubType subType = new SubType(fieldNumber, derivedMeta);
ThrowIfFrozen();
derivedMeta.SetBaseType(this); // includes ThrowIfFrozen
if (subTypes == null) subTypes = new BasicList();
subTypes.Add(subType);
return this;
}
示例2: AddSubType
/// <summary>
/// Adds a known sub-type to the inheritance model
/// </summary>
public MetaType AddSubType(int fieldNumber, Type derivedType)
{
if (!type.IsClass || type.IsSealed) {
throw new InvalidOperationException("Sub-types can only be adedd to non-sealed classes");
}
MetaType derivedMeta = model[derivedType];
SubType subType = new SubType(fieldNumber, derivedMeta);
ThrowIfFrozen();
derivedMeta.SetBaseType(this); // includes ThrowIfFrozen
if (subTypes == null) subTypes = new BasicList();
subTypes.Add(subType);
return this;
}
示例3: BuildSerializer
private IProtoTypeSerializer BuildSerializer()
{
if (type.IsEnum)
{
//return new TagDecorator(ProtoBuf.Serializer.ListItemTag, WireType.Variant, false, new EnumSerializer(type, GetEnumMap()));
return new TypeSerializer(type, new int[] { ProtoBuf.Serializer.ListItemTag },
new IProtoSerializer[] {
new TagDecorator(ProtoBuf.Serializer.ListItemTag, WireType.Variant, false, new EnumSerializer(type, GetEnumMap()))
}, null, true, true, null, constructType);
}
if (surrogate != null)
{
MetaType mt = model[surrogate], mtBase;
while ((mtBase = mt.baseType) != null) { mt = mtBase; }
return new SurrogateSerializer(type, surrogate, mt.Serializer);
}
Type itemType = TypeModel.GetListItemType(type);
if (itemType != null)
{
ValueMember fakeMember = new ValueMember(model, ProtoBuf.Serializer.ListItemTag, type, itemType, type, DataFormat.Default);
return new TypeSerializer(type, new int[] { ProtoBuf.Serializer.ListItemTag }, new IProtoSerializer[] { fakeMember.Serializer }, null, true, true, null, constructType);
}
fields.Trim();
int fieldCount = fields.Count;
int subTypeCount = subTypes == null ? 0 : subTypes.Count;
int[] fieldNumbers = new int[fieldCount + subTypeCount];
IProtoSerializer[] serializers = new IProtoSerializer[fieldCount + subTypeCount];
int i = 0;
if (subTypeCount != 0)
{
foreach (SubType subType in subTypes)
{
fieldNumbers[i] = subType.FieldNumber;
serializers[i++] = subType.Serializer;
}
}
if (fieldCount != 0)
{
foreach (ValueMember member in fields)
{
fieldNumbers[i] = member.FieldNumber;
serializers[i++] = member.Serializer;
}
}
BasicList baseCtorCallbacks = null;
MetaType tmp = BaseType;
while (tmp != null)
{
MethodInfo method = tmp.HasCallbacks ? tmp.Callbacks.BeforeDeserialize : null;
if (method != null)
{
if (baseCtorCallbacks == null) baseCtorCallbacks = new BasicList();
baseCtorCallbacks.Add(method);
}
tmp = tmp.BaseType;
}
MethodInfo[] arr = null;
if (baseCtorCallbacks != null)
{
arr = new MethodInfo[baseCtorCallbacks.Count];
baseCtorCallbacks.CopyTo(arr, 0);
Array.Reverse(arr);
}
return new TypeSerializer(type, fieldNumbers, serializers, arr, baseType == null, UseConstructor, callbacks, constructType);
}
示例4: GetContiguousGroups
internal static BasicList GetContiguousGroups(int[] keys, object[] values)
{
if (keys == null) throw new ArgumentNullException("keys");
if (values == null) throw new ArgumentNullException("values");
if (values.Length < keys.Length) throw new ArgumentException("Not all keys are covered by values", "values");
BasicList outer = new BasicList();
Group group = null;
for (int i = 0; i < keys.Length; i++)
{
if (i == 0 || keys[i] != keys[i - 1]) { group = null; }
if (group == null)
{
group = new Group(keys[i]);
outer.Add(group);
}
group.Items.Add(values[i]);
}
return outer;
}
示例5: InternalsVisible
bool InternalsVisible(Assembly assembly)
{
#if PHONE8 || SILVERLIGHT || FX11
return false;
#else
if (Helpers.IsNullOrEmpty(assemblyName)) return false;
if (knownTrustedAssemblies != null)
{
if (knownTrustedAssemblies.IndexOfReference(assembly) >= 0)
{
return true;
}
}
if (knownUntrustedAssemblies != null)
{
if (knownUntrustedAssemblies.IndexOfReference(assembly) >= 0)
{
return false;
}
}
bool isTrusted = false;
Type attributeType = MapType(typeof(System.Runtime.CompilerServices.InternalsVisibleToAttribute));
if(attributeType == null) return false;
#if FEAT_IKVM
foreach (CustomAttributeData attrib in assembly.__GetCustomAttributes(attributeType, false))
{
if (attrib.ConstructorArguments.Count == 1)
{
string privelegedAssembly = attrib.ConstructorArguments[0].Value as string;
if (privelegedAssembly == assemblyName)
{
isTrusted = true;
break;
}
}
}
#else
foreach(System.Runtime.CompilerServices.InternalsVisibleToAttribute attrib in assembly.GetCustomAttributes(attributeType, false))
{
if (attrib.AssemblyName == assemblyName)
{
isTrusted = true;
break;
}
}
#endif
if (isTrusted)
{
if (knownTrustedAssemblies == null) knownTrustedAssemblies = new BasicList();
knownTrustedAssemblies.Add(assembly);
}
else
{
if (knownUntrustedAssemblies == null) knownUntrustedAssemblies = new BasicList();
knownUntrustedAssemblies.Add(assembly);
}
return isTrusted;
#endif
}
示例6: WriteAssemblyAttributes
private void WriteAssemblyAttributes(CompilerOptions options, string assemblyName, AssemblyBuilder asm)
{
if (!Helpers.IsNullOrEmpty(options.TargetFrameworkName))
{
// get [TargetFramework] from mscorlib/equivalent and burn into the new assembly
Type versionAttribType = null;
try
{ // this is best-endeavours only
versionAttribType = GetType("System.Runtime.Versioning.TargetFrameworkAttribute", MapType(typeof(string)).Assembly);
}
catch { /* don't stress */ }
if (versionAttribType != null)
{
PropertyInfo[] props;
object[] propValues;
if (Helpers.IsNullOrEmpty(options.TargetFrameworkDisplayName))
{
props = new PropertyInfo[0];
propValues = new object[0];
}
else
{
props = new PropertyInfo[1] { versionAttribType.GetProperty("FrameworkDisplayName") };
propValues = new object[1] { options.TargetFrameworkDisplayName };
}
CustomAttributeBuilder builder = new CustomAttributeBuilder(
versionAttribType.GetConstructor(new Type[] { MapType(typeof(string)) }),
new object[] { options.TargetFrameworkName },
props,
propValues);
asm.SetCustomAttribute(builder);
}
}
// copy assembly:InternalsVisibleTo
Type internalsVisibleToAttribType = null;
#if !FX11
try
{
internalsVisibleToAttribType = MapType(typeof(System.Runtime.CompilerServices.InternalsVisibleToAttribute));
}
catch { /* best endeavors only */ }
#endif
if (internalsVisibleToAttribType != null)
{
BasicList internalAssemblies = new BasicList(), consideredAssemblies = new BasicList();
foreach (MetaType metaType in types)
{
Assembly assembly = metaType.Type.Assembly;
if (consideredAssemblies.IndexOfReference(assembly) >= 0) continue;
consideredAssemblies.Add(assembly);
AttributeMap[] assemblyAttribsMap = AttributeMap.Create(this, assembly);
for (int i = 0; i < assemblyAttribsMap.Length; i++)
{
if (assemblyAttribsMap[i].AttributeType != internalsVisibleToAttribType) continue;
object privelegedAssemblyObj;
assemblyAttribsMap[i].TryGet("AssemblyName", out privelegedAssemblyObj);
string privelegedAssemblyName = privelegedAssemblyObj as string;
if (privelegedAssemblyName == assemblyName || Helpers.IsNullOrEmpty(privelegedAssemblyName)) continue; // ignore
if (internalAssemblies.IndexOfString(privelegedAssemblyName) >= 0) continue; // seen it before
internalAssemblies.Add(privelegedAssemblyName);
CustomAttributeBuilder builder = new CustomAttributeBuilder(
internalsVisibleToAttribType.GetConstructor(new Type[] { MapType(typeof(string)) }),
new object[] { privelegedAssemblyName });
asm.SetCustomAttribute(builder);
}
}
}
}
示例7: WriteGetKeyImpl
private void WriteGetKeyImpl(TypeBuilder type, bool hasInheritance, SerializerPair[] methodPairs, Compiler.CompilerContext.ILVersion ilVersion, string assemblyName, out ILGenerator il, out int knownTypesCategory, out FieldBuilder knownTypes, out Type knownTypesLookupType)
{
il = Override(type, "GetKeyImpl");
Compiler.CompilerContext ctx = new Compiler.CompilerContext(il, false, false, methodPairs, this, ilVersion, assemblyName, MapType(typeof(System.Type), true));
if (types.Count <= KnownTypes_ArrayCutoff)
{
knownTypesCategory = KnownTypes_Array;
knownTypesLookupType = MapType(typeof(System.Type[]), true);
}
else
{
#if NO_GENERICS
knownTypesLookupType = null;
#else
knownTypesLookupType = MapType(typeof(System.Collections.Generic.Dictionary<System.Type, int>), false);
#endif
if (knownTypesLookupType == null)
{
knownTypesLookupType = MapType(typeof(Hashtable), true);
knownTypesCategory = KnownTypes_Hashtable;
}
else
{
knownTypesCategory = KnownTypes_Dictionary;
}
}
knownTypes = type.DefineField("knownTypes", knownTypesLookupType, FieldAttributes.Private | FieldAttributes.InitOnly | FieldAttributes.Static);
switch (knownTypesCategory)
{
case KnownTypes_Array:
{
il.Emit(OpCodes.Ldsfld, knownTypes);
il.Emit(OpCodes.Ldarg_1);
// note that Array.IndexOf is not supported under CF
il.EmitCall(OpCodes.Callvirt, MapType(typeof(IList)).GetMethod(
"IndexOf", new Type[] { MapType(typeof(object)) }), null);
if (hasInheritance)
{
il.DeclareLocal(MapType(typeof(int))); // loc-0
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Stloc_0);
BasicList getKeyLabels = new BasicList();
int lastKey = -1;
for (int i = 0; i < methodPairs.Length; i++)
{
if (methodPairs[i].MetaKey == methodPairs[i].BaseKey) break;
if (lastKey == methodPairs[i].BaseKey)
{ // add the last label again
getKeyLabels.Add(getKeyLabels[getKeyLabels.Count - 1]);
}
else
{ // add a new unique label
getKeyLabels.Add(ctx.DefineLabel());
lastKey = methodPairs[i].BaseKey;
}
}
Compiler.CodeLabel[] subtypeLabels = new Compiler.CodeLabel[getKeyLabels.Count];
getKeyLabels.CopyTo(subtypeLabels, 0);
ctx.Switch(subtypeLabels);
il.Emit(OpCodes.Ldloc_0); // not a sub-type; use the original value
il.Emit(OpCodes.Ret);
lastKey = -1;
// now output the different branches per sub-type (not derived type)
for (int i = subtypeLabels.Length - 1; i >= 0; i--)
{
if (lastKey != methodPairs[i].BaseKey)
{
lastKey = methodPairs[i].BaseKey;
// find the actual base-index for this base-key (i.e. the index of
// the base-type)
int keyIndex = -1;
for (int j = subtypeLabels.Length; j < methodPairs.Length; j++)
{
if (methodPairs[j].BaseKey == lastKey && methodPairs[j].MetaKey == lastKey)
{
keyIndex = j;
break;
}
}
ctx.MarkLabel(subtypeLabels[i]);
Compiler.CompilerContext.LoadValue(il, keyIndex);
il.Emit(OpCodes.Ret);
}
}
}
else
{
il.Emit(OpCodes.Ret);
}
}
break;
case KnownTypes_Dictionary:
{
LocalBuilder result = il.DeclareLocal(MapType(typeof(int)));
//.........这里部分代码省略.........
示例8: NodeEnumerator
internal NodeEnumerator(BasicList.Node node)
{
this.position = -1;
this.node = node;
}
示例9: Compile
/// <summary>
/// Fully compiles the current model into a static-compiled serialization dll
/// (the serialization dll still requires protobuf-net for support services).
/// </summary>
/// <remarks>A full compilation is restricted to accessing public types / members</remarks>
/// <param name="name">The name of the TypeModel class to create</param>
/// <param name="path">The path for the new dll</param>
/// <returns>An instance of the newly created compiled type-model</returns>
public TypeModel Compile(string name, string path)
{
BuildAllSerializers();
Freeze();
bool save = !Helpers.IsNullOrEmpty(path);
if (Helpers.IsNullOrEmpty(name))
{
if (save) throw new ArgumentNullException("name");
name = Guid.NewGuid().ToString();
}
AssemblyName an = new AssemblyName();
an.Name = name;
AssemblyBuilder asm = AppDomain.CurrentDomain.DefineDynamicAssembly(an,
(save ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run)
);
ModuleBuilder module = save ? asm.DefineDynamicModule(name, path)
: asm.DefineDynamicModule(name);
Type baseType = typeof(TypeModel);
TypeBuilder type = module.DefineType(name,
(baseType.Attributes & ~TypeAttributes.Abstract) | TypeAttributes.Sealed,
baseType);
Compiler.CompilerContext ctx;
int index = 0;
bool hasInheritance = false;
SerializerPair[] methodPairs = new SerializerPair[types.Count];
foreach (MetaType metaType in types)
{
MethodBuilder writeMethod = type.DefineMethod("Write"
#if DEBUG
+ metaType.Type.Name
#endif
,
MethodAttributes.Private | MethodAttributes.Static, CallingConventions.Standard,
typeof(void), new Type[] { metaType.Type, typeof(ProtoWriter) });
MethodBuilder readMethod = type.DefineMethod("Read"
#if DEBUG
+ metaType.Type.Name
#endif
,
MethodAttributes.Private | MethodAttributes.Static, CallingConventions.Standard,
metaType.Type, new Type[] { metaType.Type, typeof(ProtoReader) });
SerializerPair pair = new SerializerPair(
GetKey(metaType.Type, true, false), GetKey(metaType.Type, true, true), metaType,
writeMethod, readMethod, writeMethod.GetILGenerator(), readMethod.GetILGenerator());
methodPairs[index++] = pair;
if (pair.MetaKey != pair.BaseKey) hasInheritance = true;
}
if (hasInheritance)
{
Array.Sort(methodPairs);
}
for(index = 0; index < methodPairs.Length ; index++)
{
SerializerPair pair = methodPairs[index];
ctx = new Compiler.CompilerContext(pair.SerializeBody, true, true, methodPairs);
pair.Type.Serializer.EmitWrite(ctx, Compiler.Local.InputValue);
ctx.Return();
ctx = new Compiler.CompilerContext(pair.DeserializeBody, true, false, methodPairs);
pair.Type.Serializer.EmitRead(ctx, Compiler.Local.InputValue);
if (!pair.Type.Serializer.ReturnsValue)
{
ctx.LoadValue(Compiler.Local.InputValue);
}
ctx.Return();
}
FieldBuilder knownTypes = type.DefineField("knownTypes", typeof(Type[]), FieldAttributes.Private | FieldAttributes.InitOnly);
ILGenerator il = Override(type, "GetKeyImpl");
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, knownTypes);
il.Emit(OpCodes.Ldarg_1);
// note that Array.IndexOf is not supported under CF
il.EmitCall(OpCodes.Callvirt,typeof(IList).GetMethod(
"IndexOf", new Type[] { typeof(object) }), null);
if (hasInheritance)
{
il.DeclareLocal(typeof(int)); // loc-0
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Stloc_0);
BasicList getKeyLabels = new BasicList();
int lastKey = -1;
for (int i = 0; i < methodPairs.Length; i++)
{
//.........这里部分代码省略.........
示例10: ApplyDefaultBehaviour
private ValueMember ApplyDefaultBehaviour(bool isEnum, AttributeFamily family, MemberInfo member, BasicList partialMembers)
{
if (member == null || (family == AttributeFamily.None && !isEnum)) return null; // nix
Type effectiveType;
switch (member.MemberType)
{
case MemberTypes.Field:
effectiveType = ((FieldInfo)member).FieldType; break;
case MemberTypes.Property:
effectiveType = ((PropertyInfo)member).PropertyType; break;
default:
return null; // nothing doing
}
int fieldNumber = 0;
bool isPacked = false;
string name = null;
bool isRequired = false;
Type itemType = null;
Type defaultType = null;
ResolveListTypes(effectiveType, ref itemType, ref defaultType);
object[] attribs = member.GetCustomAttributes(true);
Attribute attrib;
DataFormat dataFormat = DataFormat.Default;
bool ignore = false;
object defaultValue = null;
// implicit zero default
switch (Type.GetTypeCode(effectiveType))
{
case TypeCode.Boolean: defaultValue = false; break;
case TypeCode.Decimal: defaultValue = (decimal)0; break;
case TypeCode.Single: defaultValue = (float)0; break;
case TypeCode.Double: defaultValue = (double)0; break;
case TypeCode.Byte: defaultValue = (byte)0; break;
case TypeCode.Char: defaultValue = (char)0; break;
case TypeCode.Int16: defaultValue = (short)0; break;
case TypeCode.Int32: defaultValue = (int)0; break;
case TypeCode.Int64: defaultValue = (long)0; break;
case TypeCode.SByte: defaultValue = (sbyte)0; break;
case TypeCode.UInt16: defaultValue = (ushort)0; break;
case TypeCode.UInt32: defaultValue = (uint)0; break;
case TypeCode.UInt64: defaultValue = (ulong)0; break;
default:
if (effectiveType == typeof(TimeSpan)) defaultValue = TimeSpan.Zero;
if (effectiveType == typeof(Guid)) defaultValue = Guid.Empty;
break;
}
bool done = false;
if (isEnum)
{
attrib = GetAttribute(attribs, "ProtoBuf.ProtoIgnoreAttribute");
if (attrib != null)
{
ignore = true;
}
else
{
attrib = GetAttribute(attribs, "ProtoBuf.ProtoEnumAttribute");
fieldNumber = Convert.ToInt32(((FieldInfo)member).GetValue(null));
if (attrib != null)
{
GetFieldName(ref name, attrib, "Name");
if ((bool)attrib.GetType().GetMethod("HasValue").Invoke(attrib, null))
{
fieldNumber = (int) GetMemberValue(attrib, "Value");
}
}
}
done = true;
}
if (!ignore && !done) // always consider ProtoMember
{
attrib = GetAttribute(attribs, "ProtoBuf.ProtoMemberAttribute");
GetIgnore(ref ignore, attrib, attribs, "ProtoBuf.ProtoIgnoreAttribute");
if (!ignore)
{
GetFieldNumber(ref fieldNumber, attrib, "Tag");
GetFieldName(ref name, attrib, "Name");
GetFieldBoolean(ref isRequired, attrib, "IsRequired");
GetFieldBoolean(ref isPacked, attrib, "IsPacked");
GetDataFormat(ref dataFormat, attrib, "DataFormat");
done = fieldNumber > 0;
}
if (!done && partialMembers != null)
{
foreach (ProtoPartialMemberAttribute ppma in partialMembers)
{
if (ppma.MemberName == member.Name)
{
GetFieldNumber(ref fieldNumber, ppma, "Tag");
GetFieldName(ref name, ppma, "Name");
GetFieldBoolean(ref isRequired, ppma, "IsRequired");
GetFieldBoolean(ref isPacked, ppma, "IsPacked");
GetDataFormat(ref dataFormat, ppma, "DataFormat");
if (done = fieldNumber > 0) break;
}
}
}
//.........这里部分代码省略.........
示例11: BuildSerializer
private IProtoTypeSerializer BuildSerializer()
{
if (surrogate != null) return new SurrogateSerializer(type, model[surrogate].Serializer);
fields.Trim();
int fieldCount = fields.Count;
int subTypeCount = subTypes == null ? 0 : subTypes.Count;
int[] fieldNumbers = new int[fieldCount + subTypeCount];
IProtoSerializer[] serializers = new IProtoSerializer[fieldCount + subTypeCount];
int i = 0;
if (subTypeCount != 0)
{
foreach (SubType subType in subTypes)
{
fieldNumbers[i] = subType.FieldNumber;
serializers[i++] = subType.Serializer;
}
}
if (fieldCount != 0)
{
foreach (ValueMember member in fields)
{
fieldNumbers[i] = member.FieldNumber;
serializers[i++] = member.Serializer;
}
}
BasicList baseCtorCallbacks = null;
MetaType tmp = BaseType;
while (tmp != null)
{
MethodInfo method = tmp.HasCallbacks ? tmp.Callbacks.BeforeDeserialize : null;
if (method != null)
{
if (baseCtorCallbacks == null) baseCtorCallbacks = new BasicList();
baseCtorCallbacks.Add(method);
}
tmp = tmp.BaseType;
}
MethodInfo[] arr = null;
if (baseCtorCallbacks != null)
{
arr = new MethodInfo[baseCtorCallbacks.Count];
baseCtorCallbacks.CopyTo(arr, 0);
Array.Reverse(arr);
}
return new TypeSerializer(type, fieldNumbers, serializers, arr, baseType == null, useConstructor, callbacks);
}
示例12: ReadSInstance
void ReadSInstance(ProtoReader reader, SInstance sInstance, CLS_Environment environment)
{
List<CLS_Content.Value> values;
List<string> keywords;
GetSortMembers(sInstance, out values, out keywords);
int fieldNumber = 0;
while ((fieldNumber = reader.ReadFieldHeader()) > 0)
{
Type memberT = values[fieldNumber - 1].type;
CLS_Content.Value memberV = values[fieldNumber - 1];
string sClassName = keywords[fieldNumber - 1];
if (memberT == null)
{
memberT = typeof(SInstance);
sClassName = ((SType)memberV.type).Name;
}
Type itemType = GetItemType(memberT);
if (itemType != null)
{
sClassName = sInstance.type.members[sClassName].type.keyword;
// 数组判断
if (memberT.IsArray)
{
string itemClass = sClassName.Substring(0, sClassName.Length - 2); // 从 xxx[] 中提取xxx
BasicList list = new BasicList();
do
{
list.Add(ReadField(reader, itemType, itemClass, environment));
} while (reader.TryReadFieldHeader(fieldNumber));
Array result = Array.CreateInstance(itemType, list.Count);
list.CopyTo(result, 0);
memberV.value = result;
}
// 列表判断
else
{
string itemClass = sClassName.Substring(5, sClassName.Length - 6); // 从 List<xxx> 中提取xxx
ICLS_Type iType = environment.GetTypeByKeywordQuiet(sClassName);
CLS_Content content = CLS_Content.NewContent(environment);
memberV.value = iType.function.New(content, m_emptyParams).value;
CLS_Content.PoolContent(content);
IList list = (IList)memberV.value;
do
{
list.Add(ReadField(reader, itemType, itemClass, environment));
} while (reader.TryReadFieldHeader(fieldNumber));
}
}
else
{
memberV.value = ReadField(reader, memberT, sClassName, environment);
}
}
}
示例13: NormalizeProtoMember
private static ProtoMemberAttribute NormalizeProtoMember(MemberInfo member, AttributeFamily family, bool forced, bool isEnum, BasicList partialMembers, int dataMemberOffset, bool inferByTagName)
{
if (member == null || (family == AttributeFamily.None && !isEnum)) return null; // nix
int fieldNumber = int.MinValue, minAcceptFieldNumber = inferByTagName ? -1 : 1;
string name = null;
bool isPacked = false, ignore = false, done = false, isRequired = false, asReference = false, dynamicType = false, tagIsPinned = false, overwriteList = false;
DataFormat dataFormat = DataFormat.Default;
if (isEnum) forced = true;
object[] attribs = member.GetCustomAttributes(true);
Attribute attrib;
if (isEnum)
{
attrib = GetAttribute(attribs, "ProtoBuf.ProtoIgnoreAttribute");
if (attrib != null)
{
ignore = true;
}
else
{
attrib = GetAttribute(attribs, "ProtoBuf.ProtoEnumAttribute");
fieldNumber = Convert.ToInt32(((FieldInfo)member).GetValue(null));
if (attrib != null)
{
GetFieldName(ref name, attrib, "Name");
if ((bool)attrib.GetType().GetMethod("HasValue").Invoke(attrib, null))
{
fieldNumber = (int)GetMemberValue(attrib, "Value");
}
}
}
done = true;
}
if (!ignore && !done) // always consider ProtoMember
{
attrib = GetAttribute(attribs, "ProtoBuf.ProtoMemberAttribute");
GetIgnore(ref ignore, attrib, attribs, "ProtoBuf.ProtoIgnoreAttribute");
if (!ignore && attrib != null)
{
GetFieldNumber(ref fieldNumber, attrib, "Tag");
GetFieldName(ref name, attrib, "Name");
GetFieldBoolean(ref isRequired, attrib, "IsRequired");
GetFieldBoolean(ref isPacked, attrib, "IsPacked");
GetFieldBoolean(ref overwriteList, attrib, "OverwriteList");
GetDataFormat(ref dataFormat, attrib, "DataFormat");
GetFieldBoolean(ref asReference, attrib, "AsReference");
GetFieldBoolean(ref dynamicType, attrib, "DynamicType");
done = tagIsPinned = fieldNumber > 0; // note minAcceptFieldNumber only applies to non-proto
}
if (!done && partialMembers != null)
{
foreach (ProtoPartialMemberAttribute ppma in partialMembers)
{
if (ppma.MemberName == member.Name)
{
GetFieldNumber(ref fieldNumber, ppma, "Tag");
GetFieldName(ref name, ppma, "Name");
GetFieldBoolean(ref isRequired, ppma, "IsRequired");
GetFieldBoolean(ref isPacked, ppma, "IsPacked");
GetFieldBoolean(ref overwriteList, attrib, "OverwriteList");
GetDataFormat(ref dataFormat, ppma, "DataFormat");
GetFieldBoolean(ref asReference, ppma, "AsReference");
GetFieldBoolean(ref dynamicType, ppma, "DynamicType");
if (done = tagIsPinned = fieldNumber > 0) break; // note minAcceptFieldNumber only applies to non-proto
}
}
}
}
if (!ignore && !done && HasFamily(family, AttributeFamily.DataContractSerialier))
{
attrib = GetAttribute(attribs, "System.Runtime.Serialization.DataMemberAttribute");
if (attrib != null)
{
GetFieldNumber(ref fieldNumber, attrib, "Order");
GetFieldName(ref name, attrib, "Name");
GetFieldBoolean(ref isRequired, attrib, "IsRequired");
done = fieldNumber >= minAcceptFieldNumber;
if (done) fieldNumber += dataMemberOffset; // dataMemberOffset only applies to DCS flags, to allow us to "bump" WCF by a notch
}
}
if (!ignore && !done && HasFamily(family, AttributeFamily.XmlSerializer))
{
attrib = GetAttribute(attribs, "System.Xml.Serialization.XmlElementAttribute");
if(attrib == null) attrib = GetAttribute(attribs, "System.Xml.Serialization.XmlArrayAttribute");
GetIgnore(ref ignore, attrib, attribs, "System.Xml.Serialization.XmlIgnoreAttribute");
if (attrib != null && !ignore)
{
GetFieldNumber(ref fieldNumber, attrib, "Order");
GetFieldName(ref name, attrib, "ElementName");
done = fieldNumber >= minAcceptFieldNumber;
}
}
if (!ignore && !done)
{
if (GetAttribute(attribs, "System.NonSerializedAttribute") != null) ignore = true;
}
//.........这里部分代码省略.........
示例14: ApplyDefaultBehaviour
internal void ApplyDefaultBehaviour()
{
if (type.BaseType != null && model.FindWithoutAdd(type.BaseType) == null
&& GetContractFamily(type.BaseType, null) != MetaType.AttributeFamily.None)
{
model.FindOrAddAuto(type.BaseType, true, false, false);
}
object[] typeAttribs = type.GetCustomAttributes(true);
AttributeFamily family = GetContractFamily(type, typeAttribs);
bool isEnum = type.IsEnum;
if(family == AttributeFamily.None && !isEnum) return; // and you'd like me to do what, exactly?
BasicList partialIgnores = null, partialMembers = null;
int dataMemberOffset = 0, implicitFirstTag = 1;
bool inferTagByName = model.InferTagFromNameDefault;
ImplicitFields implicitMode = ImplicitFields.None;
for (int i = 0; i < typeAttribs.Length; i++)
{
Attribute item = (Attribute)typeAttribs[i];
if (!isEnum && item is ProtoIncludeAttribute)
{
ProtoIncludeAttribute pia = (ProtoIncludeAttribute)item;
Type knownType = pia.ResolveKnownType(type.Assembly);
if (knownType == null)
{
throw new InvalidOperationException("Unable to resolve sub-type: " + pia.KnownTypeName);
}
AddSubType(pia.Tag, knownType);
}
if(item is ProtoPartialIgnoreAttribute)
{
if(partialIgnores == null) partialIgnores = new BasicList();
partialIgnores.Add(((ProtoPartialIgnoreAttribute)item).MemberName);
}
if (!isEnum && item is ProtoPartialMemberAttribute)
{
if (partialMembers == null) partialMembers = new BasicList();
partialMembers.Add(item);
}
if (!isEnum && item is ProtoContractAttribute)
{
ProtoContractAttribute pca = (ProtoContractAttribute)item;
dataMemberOffset = pca.DataMemberOffset;
if (pca.InferTagFromNameHasValue) inferTagByName = pca.InferTagFromName;
implicitMode = pca.ImplicitFields;
UseConstructor = !pca.SkipConstructor;
if(pca.ImplicitFirstTag > 0) implicitFirstTag = pca.ImplicitFirstTag;
}
}
if (implicitMode != ImplicitFields.None)
{
family &= AttributeFamily.ProtoBuf; // with implicit fields, **only** proto attributes are important
}
MethodInfo[] callbacks = null;
BasicList members = new BasicList();
foreach (MemberInfo member in type.GetMembers(isEnum ? BindingFlags.Public | BindingFlags.Static
: BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
if (member.DeclaringType != type) continue;
if (member.IsDefined(typeof(ProtoIgnoreAttribute), true)) continue;
if (partialIgnores != null && partialIgnores.Contains(member.Name)) continue;
bool forced = false, isPublic, isField;
Type effectiveType;
switch (member.MemberType)
{
case MemberTypes.Property:
PropertyInfo property = (PropertyInfo)member;
effectiveType = property.PropertyType;
isPublic = property.GetGetMethod(false) != null;
isField = false;
goto ProcessMember;
case MemberTypes.Field:
FieldInfo field = (FieldInfo)member;
effectiveType = field.FieldType;
isPublic = field.IsPublic;
isField = true;
ProcessMember:
switch(implicitMode)
{
case ImplicitFields.AllFields:
if (isField) forced = true;
break;
case ImplicitFields.AllPublic:
if (isPublic) forced = true;
break;
}
if (effectiveType.IsSubclassOf(typeof(Delegate))) continue; // we just don't like delegate types ;p
ProtoMemberAttribute normalizedAttribute = NormalizeProtoMember(member, family, forced, isEnum, partialMembers, dataMemberOffset, inferTagByName);
if(normalizedAttribute != null) members.Add(normalizedAttribute);
break;
case MemberTypes.Method:
if (isEnum) continue;
MethodInfo method = (MethodInfo)member;
object[] memberAttribs = Attribute.GetCustomAttributes(method);
if (memberAttribs != null && memberAttribs.Length > 0)
{
//.........这里部分代码省略.........
示例15: Read
public override object Read(object value, ProtoReader source)
{
if (nested)
{
source.ReadFieldHeader();
}
int field = source.FieldNumber;
BasicList list = new BasicList();
if (packedWireType != WireType.None && source.WireType == WireType.String)
{
SubItemToken token = ProtoReader.StartSubItem(source);
while (ProtoReader.HasSubValue(packedWireType, source))
{
list.Add(Tail.Read(null, source));
}
ProtoReader.EndSubItem(token, source);
int oldLen = AppendToCollection ? ((value == null ? 0 : ((Array)value).Length)) : 0;
Array result = Array.CreateInstance(itemType, oldLen + list.Count);
if (oldLen != 0) ((Array)value).CopyTo(result, 0);
list.CopyTo(result, oldLen);
return result;
}
else
{
bool readObject = true;
int arrayKey = 0;
value = value ?? Array.CreateInstance(itemType, 0);
if (AsReference)
{
int objectKey = source.ReadInt32();
if (objectKey > 0)
{
value = source.NetCache.GetKeyedObject(objectKey);
readObject = false;
}
else
{
bool dummy;
arrayKey = source.NetCache.AddObjectKey(value, out dummy);
}
}
else
{
bool isEmpty = source.ReadInt32() == 1;
if (isEmpty)
{
return value;
}
}
if (readObject)
{
while (source.TryReadFieldHeader(field))
{
list.Add(Tail.Read(null, source));
}
Array newArray = Array.CreateInstance(itemType, list.Count);
list.CopyTo(newArray, 0);
if (AsReference)
{
source.NetCache.UpdateKeyedObject(arrayKey, value, newArray);
}
value = newArray;
}
}
return value;
}