本文整理汇总了C#中ProtoBuf.Meta.BasicList.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# BasicList.CopyTo方法的具体用法?C# BasicList.CopyTo怎么用?C# BasicList.CopyTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ProtoBuf.Meta.BasicList
的用法示例。
在下文中一共展示了BasicList.CopyTo方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSchema
/// <summary>
/// Suggest a .proto definition for the given type
/// </summary>
/// <param name="type">The type to generate a .proto definition for, or <c>null</c> to generate a .proto that represents the entire model</param>
/// <returns>The .proto definition as a string</returns>
public override string GetSchema(Type type)
{
BasicList requiredTypes = new BasicList();
MetaType primaryType = null;
bool isInbuiltType = false;
if (type == null)
{ // generate for the entire model
foreach(MetaType meta in types)
{
MetaType tmp = meta.GetSurrogateOrBaseOrSelf(false);
if (!requiredTypes.Contains(tmp))
{ // ^^^ note that the type might have been added as a descendent
requiredTypes.Add(tmp);
CascadeDependents(requiredTypes, tmp);
}
}
}
else
{
Type tmp = Helpers.GetUnderlyingType(type);
if (tmp != null) type = tmp;
WireType defaultWireType;
isInbuiltType = (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, type, out defaultWireType, false, false, false, false) != null);
if (!isInbuiltType)
{
//Agenerate just relative to the supplied type
int index = FindOrAddAuto(type, false, false, false);
if (index < 0) throw new ArgumentException("The type specified is not a contract-type", "type");
// get the required types
primaryType = ((MetaType)types[index]).GetSurrogateOrBaseOrSelf(false);
requiredTypes.Add(primaryType);
CascadeDependents(requiredTypes, primaryType);
}
}
// use the provided type's namespace for the "package"
StringBuilder headerBuilder = new StringBuilder();
string package = null;
if (!isInbuiltType)
{
IEnumerable typesForNamespace = primaryType == null ? types : requiredTypes;
foreach (MetaType meta in typesForNamespace)
{
if (meta.IsList) continue;
string tmp = meta.Type.Namespace;
if (!Helpers.IsNullOrEmpty(tmp))
{
if (tmp.StartsWith("System.")) continue;
if (package == null)
{ // haven't seen any suggestions yet
package = tmp;
}
else if (package == tmp)
{ // that's fine; a repeat of the one we already saw
}
else
{ // something else; have confliucting suggestions; abort
package = null;
break;
}
}
}
}
if (!Helpers.IsNullOrEmpty(package))
{
headerBuilder.Append("package ").Append(package).Append(';');
Helpers.AppendLine(headerBuilder);
}
bool requiresBclImport = false;
StringBuilder bodyBuilder = new StringBuilder();
// sort them by schema-name
MetaType[] metaTypesArr = new MetaType[requiredTypes.Count];
requiredTypes.CopyTo(metaTypesArr, 0);
Array.Sort(metaTypesArr, MetaType.Comparer.Default);
// write the messages
if (isInbuiltType)
{
Helpers.AppendLine(bodyBuilder).Append("message ").Append(type.Name).Append(" {");
MetaType.NewLine(bodyBuilder, 1).Append("optional ").Append(GetSchemaTypeName(type, DataFormat.Default, false, false, ref requiresBclImport))
.Append(" value = 1;");
Helpers.AppendLine(bodyBuilder).Append('}');
}
else
{
for (int i = 0; i < metaTypesArr.Length; i++)
{
MetaType tmp = metaTypesArr[i];
if (tmp.IsList && tmp != primaryType) continue;
tmp.WriteSchema(bodyBuilder, 0, ref requiresBclImport);
//.........这里部分代码省略.........
示例2: Read
public override object Read(object value, ProtoReader source)
{
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);
}
else
{
do
{
list.Add(Tail.Read(null, source));
} while (source.TryReadFieldHeader(field));
}
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;
}
示例3: Compile
//.........这里部分代码省略.........
}
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++)
{
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(il.DefineLabel());
lastKey = methodPairs[i].BaseKey;
}
}
Label[] subtypeLabels = new Label[getKeyLabels.Count];
getKeyLabels.CopyTo(subtypeLabels, 0);
il.Emit(OpCodes.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;
}
}
il.MarkLabel(subtypeLabels[i]);
Compiler.CompilerContext.LoadValue(il, keyIndex);
il.Emit(OpCodes.Ret);
}
}
}
else
{
il.Emit(OpCodes.Ret);
示例4: 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)));
//.........这里部分代码省略.........
示例5: 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;
}
示例6: Compile
//.........这里部分代码省略.........
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(il.DefineLabel());
lastKey = methodPairs[i].BaseKey;
}
}
Label[] subtypeLabels = new Label[getKeyLabels.Count];
getKeyLabels.CopyTo(subtypeLabels, 0);
il.Emit(OpCodes.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;
}
}
il.MarkLabel(subtypeLabels[i]);
Compiler.CompilerContext.LoadValue(il, keyIndex);
il.Emit(OpCodes.Ret);
}
}
}
else
{
il.Emit(OpCodes.Ret);
示例7: GetSchema
public override string GetSchema(Type type)
{
BasicList basicList = new BasicList();
MetaType metaType = null;
bool flag = false;
if (type == null)
{
BasicList.NodeEnumerator enumerator = this.types.GetEnumerator();
while (enumerator.MoveNext())
{
MetaType metaType2 = (MetaType)enumerator.Current;
MetaType surrogateOrBaseOrSelf = metaType2.GetSurrogateOrBaseOrSelf(false);
if (!basicList.Contains(surrogateOrBaseOrSelf))
{
basicList.Add(surrogateOrBaseOrSelf);
this.CascadeDependents(basicList, surrogateOrBaseOrSelf);
}
}
}
else
{
Type underlyingType = Helpers.GetUnderlyingType(type);
if (underlyingType != null)
{
type = underlyingType;
}
WireType wireType;
flag = (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, type, out wireType, false, false, false, false) != null);
if (!flag)
{
int num = this.FindOrAddAuto(type, false, false, false);
if (num < 0)
{
throw new ArgumentException("The type specified is not a contract-type", "type");
}
metaType = ((MetaType)this.types[num]).GetSurrogateOrBaseOrSelf(false);
basicList.Add(metaType);
this.CascadeDependents(basicList, metaType);
}
}
StringBuilder stringBuilder = new StringBuilder();
string text = null;
if (!flag)
{
IEnumerable enumerable = (metaType != null) ? basicList : this.types;
foreach (MetaType metaType3 in enumerable)
{
if (!metaType3.IsList)
{
string @namespace = metaType3.Type.Namespace;
if (!Helpers.IsNullOrEmpty(@namespace))
{
if ([email protected]("System."))
{
if (text == null)
{
text = @namespace;
}
else if (!(text == @namespace))
{
text = null;
break;
}
}
}
}
}
}
if (!Helpers.IsNullOrEmpty(text))
{
stringBuilder.Append("package ").Append(text).Append(';');
Helpers.AppendLine(stringBuilder);
}
bool flag2 = false;
StringBuilder stringBuilder2 = new StringBuilder();
MetaType[] array = new MetaType[basicList.Count];
basicList.CopyTo(array, 0);
Array.Sort<MetaType>(array, MetaType.Comparer.Default);
if (flag)
{
Helpers.AppendLine(stringBuilder2).Append("message ").Append(type.Name).Append(" {");
MetaType.NewLine(stringBuilder2, 1).Append("optional ").Append(this.GetSchemaTypeName(type, DataFormat.Default, false, false, ref flag2)).Append(" value = 1;");
Helpers.AppendLine(stringBuilder2).Append('}');
}
else
{
for (int i = 0; i < array.Length; i++)
{
MetaType metaType4 = array[i];
if (!metaType4.IsList || metaType4 == metaType)
{
metaType4.WriteSchema(stringBuilder2, 0, ref flag2);
}
}
}
if (flag2)
{
stringBuilder.Append("import \"bcl.proto\"; // schema for protobuf-net's handling of core .NET types");
Helpers.AppendLine(stringBuilder);
}
//.........这里部分代码省略.........
示例8: 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);
}
}
}
示例9: ApplyDefaultBehaviour
//.........这里部分代码省略.........
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)
{
CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeSerializationAttribute", ref callbacks, 0);
CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterSerializationAttribute", ref callbacks, 1);
CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeDeserializationAttribute", ref callbacks, 2);
CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterDeserializationAttribute", ref callbacks, 3);
CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializingAttribute", ref callbacks, 4);
CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializedAttribute", ref callbacks, 5);
CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializingAttribute", ref callbacks, 6);
CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializedAttribute", ref callbacks, 7);
}
break;
}
}
ProtoMemberAttribute[] arr = new ProtoMemberAttribute[members.Count];
members.CopyTo(arr, 0);
if (inferTagByName || implicitMode != ImplicitFields.None)
{
Array.Sort(arr);
int nextTag = implicitFirstTag;
foreach (ProtoMemberAttribute normalizedAttribute in arr)
{
if (!normalizedAttribute.TagIsPinned) // if ProtoMember etc sets a tag, we'll trust it
{
normalizedAttribute.Rebase(nextTag++);
}
}
}
foreach (ProtoMemberAttribute normalizedAttribute in arr)
{
ValueMember vm = ApplyDefaultBehaviour(isEnum, normalizedAttribute);
if (vm != null)
{
Add(vm);
}
}
if (callbacks != null)
{
SetCallbacks(Coalesce(callbacks, 0, 4), Coalesce(callbacks, 1, 5),
Coalesce(callbacks, 2, 6), Coalesce(callbacks, 3, 7));
}
}
示例10: Read
public override object Read(object value, ProtoReader source)
{
int field = source.FieldNumber;
BasicList basicList = new BasicList();
if (this.packedWireType != WireType.None && source.WireType == WireType.String)
{
SubItemToken token = ProtoReader.StartSubItem(source);
while (ProtoReader.HasSubValue(this.packedWireType, source))
{
basicList.Add(this.Tail.Read(null, source));
}
ProtoReader.EndSubItem(token, source);
}
else
{
do
{
basicList.Add(this.Tail.Read(null, source));
}
while (source.TryReadFieldHeader(field));
}
int num = (!this.AppendToCollection) ? 0 : ((value != null) ? ((Array)value).Length : 0);
Array array = Array.CreateInstance(this.itemType, num + basicList.Count);
if (num != 0)
{
((Array)value).CopyTo(array, 0);
}
basicList.CopyTo(array, num);
return array;
}
示例11: ResolveTupleConstructor
private static ConstructorInfo ResolveTupleConstructor(Type type, out MemberInfo[] mappedMembers)
{
mappedMembers = null;
if(type == null) throw new ArgumentNullException("type");
if(type.IsAbstract) return null; // as if!
ConstructorInfo[] ctors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
// need to have an interesting constructor to bother even checking this stuff
if(ctors.Length == 0 || (ctors.Length == 1 && ctors[0].GetParameters().Length == 0)) return null;
MemberInfo[] membersUnfiltered = type.GetMembers(BindingFlags.Public | BindingFlags.Instance);
BasicList memberList = new BasicList();
for(int i = 0 ; i < membersUnfiltered.Length ; i++)
{
switch(membersUnfiltered[i].MemberType)
{
case MemberTypes.Field:
if (!((FieldInfo)membersUnfiltered[i]).IsInitOnly) return null; // all public fields must be readonly to be counted a tuple
memberList.Add(membersUnfiltered[i]);
break;
case MemberTypes.Property:
PropertyInfo prop = (PropertyInfo) membersUnfiltered[i];
if (!prop.CanRead || prop.CanWrite) return null; // all properties must be read-only to be counted a tuple
memberList.Add(membersUnfiltered[i]);
break;
}
}
if (memberList.Count == 0)
{
return null;
}
MemberInfo[] members = new MemberInfo[memberList.Count];
memberList.CopyTo(members, 0);
int[] mapping = new int[members.Length];
int found = 0;
ConstructorInfo result = null;
mappedMembers = new MemberInfo[mapping.Length];
for(int i = 0 ; i < ctors.Length ; i++)
{
ParameterInfo[] parameters = ctors[i].GetParameters();
if (parameters.Length != members.Length) continue;
// reset the mappings to test
for (int j = 0; j < mapping.Length; j++) mapping[j] = -1;
for(int j = 0 ; j < parameters.Length ; j++)
{
string lower = parameters[j].Name.ToLower();
for(int k = 0 ; k < members.Length ; k++)
{
if (members[k].Name.ToLower() != lower) continue;
Type memberType;
switch(members[k].MemberType)
{
case MemberTypes.Field:
memberType = ((FieldInfo) members[k]).FieldType;
break;
case MemberTypes.Property:
memberType = ((PropertyInfo)members[k]).PropertyType;
break;
default:
memberType = null;
break;
}
if (memberType != parameters[j].ParameterType) continue;
mapping[j] = k;
}
}
// did we map all?
bool notMapped = false;
for (int j = 0; j < mapping.Length; j++)
{
if (mapping[j] < 0)
{
notMapped = true;
break;
}
mappedMembers[j] = members[mapping[j]];
}
if (notMapped) continue;
found++;
result = ctors[i];
}
return found == 1 ? result : null;
}