private static void SetCollectionValueStatic(ref object collection, int collectionIndex, object memberValue) { var hashSet = collection as IEnumerable; var type = hashSet.GetType(); var countField = type.GetProperty("Count", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(); int count = (int)countField.Invoke(collection); var memberType = type.GetGenericArguments()[0]; var arrayType = memberType.MakeArrayType(); var copyToMethod = type.GetMethod("CopyTo", ArrayExtensions.TempTypeArray(arrayType)); var values = Array.CreateInstance(memberType, count); copyToMethod.InvokeWithParameter(collection, values); var clearMethod = type.GetMethod("Clear", BindingFlags.Public | BindingFlags.Instance); clearMethod.Invoke(collection); var add = type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);; for (int n = 0; n < collectionIndex; n++) { add.InvokeWithParameter(collection, values.GetValue(n)); } add.InvokeWithParameter(collection, memberValue); for (int n = collectionIndex + 1; n < count; n++) { add.InvokeWithParameter(collection, values.GetValue(n)); } }
/// <inheritdoc/> protected override bool TryToManuallyUpdateCachedValueFromMember(int memberIndex, object memberValue, LinkedMemberInfo memberLinkedMemberInfo) { if (ReadOnly) { #if DEV_MODE Debug.LogWarning(ToString() + ".TryToManuallyUpdateCachedValueFromMember was called for parent which was ReadOnly. This should not be possible - unless external scripts caused the value change."); #endif return(false); } var parameterTypes = ArrayExtensions.TempTypeArray(memberBuildList[0].Type, memberBuildList[1].Type); var parameters = ArrayExtensions.TempObjectArray(null, null); var type = Type; var constructor = type.GetConstructor(parameterTypes); var setValues = GetValues(); for (int n = setValues.Length - 1; n >= 0; n--) { if (memberIndex == 0) { parameters[0] = memberValue; parameters[1] = members[1].GetValue(n); } else { parameters[0] = members[0].GetValue(n); parameters[1] = memberValue; } setValues[n] = constructor.Invoke(parameters); } SetValues(setValues, true, false); return(true); }
/// <inheritdoc/> protected override IDictionary GetCopyOfValue(IDictionary source) { if (source == null) { return(null); } var type = source.GetType(); Type keyType; Type valueType; if (!TryGetMemberTypes(type, out keyType, out valueType)) { #if DEV_MODE Debug.LogError(ToString() + ".GetCopyOfValue failed for source of type " + StringUtils.TypeToString(source)); #endif return(source); } var equalityComparerType = typeof(IEqualityComparer <>).MakeGenericType(keyType); var parameterTypes = ArrayExtensions.TempTypeArray(type, equalityComparerType); //try to get the constructor Dictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) var constructor = type.GetConstructor(parameterTypes); if (constructor != null) { var parameterValues = ArrayExtensions.TempObjectArray(source, null); #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(parameterValues.Length == constructor.GetParameters().Length); #endif var result = (IDictionary)constructor.Invoke(parameterValues); #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(result.Count == source.Count); Debug.Assert(result.ContentsMatch(source)); #endif return(result); } var instance = type.DefaultValue() as IDictionary; if (instance != null) { foreach (var key in source.Keys) { instance.Add(key, source[key]); } return(instance); } #if DEV_MODE Debug.LogError(ToString() + ".GetCopyOfValue failed to find Dictionary constructor that takes " + StringUtils.ToString(parameterTypes) + " as parameter."); #endif return(source); }
/// <inheritdoc/> protected override IList GetCopyOfValue(IList source) { if (source == null) { return(null); } var type = source.GetType(); Type listMemberType; if (!TryGetMemberType(type, out listMemberType)) { #if DEV_MODE Debug.LogError(ToString() + ".GetCopyOfValue failed for source of type " + StringUtils.TypeToString(source)); #endif return(source); } var parameterTypeGeneric = typeof(IEnumerable <>); var parameterType = parameterTypeGeneric.MakeGenericType(listMemberType); var parameterTypes = ArrayExtensions.TempTypeArray(parameterType); var constructor = type.GetConstructor(parameterTypes); if (constructor != null) { var parameterValues = ArrayExtensions.TempObjectArray(source); var result = (IList)constructor.Invoke(parameterValues); #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(result.Count == source.Count); Debug.Assert(result.ContentsMatch(source)); #endif return(result); } var instance = type.DefaultValue() as IList; if (instance != null) { for (int n = 0, count = source.Count; n < count; n++) { instance.Add(source[n]); } return(instance); } #if DEV_MODE Debug.LogError(ToString() + ".GetCopyOfValue failed to find List constructor that takes " + StringUtils.ToString(parameterType) + " as parameter."); #endif return(source); }