/// <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); }
public static void AddTab([NotNull] this EditorWindow window, Type editorWindowType) { var dockArea = window.ParentHostView(); var dockAreaType = Types.GetInternalEditorType("UnityEditor.DockArea"); var addTabMethod = dockAreaType.GetMethod("AddTabToHere", BindingFlags.NonPublic | BindingFlags.Instance); addTabMethod.Invoke(dockArea, ArrayExtensions.TempObjectArray(editorWindowType)); }
/// <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); }
/// <inheritdoc/> public override void OnMemberValueChanged(int memberIndex, object memberValue, LinkedMemberInfo memberLinkedMemberInfo) { // If value is not valid, don't accept it. if (memberIndex == 0 && !ValidateKey(ArrayExtensions.TempObjectArray(memberValue))) { var valueWas = Value.Key; if (valueWas != memberValue) { memberValue = Value.Key; return; //abort immediately } } base.OnMemberValueChanged(memberIndex, memberValue, memberLinkedMemberInfo); }
public static void RemoveAt([NotNull] ref ICollection collection, int index, bool throwExceptionIfFails) { int count; try { count = collection.Count; } catch (Exception) { if (throwExceptionIfFails) { throw; } return; } if (count <= index) { if (throwExceptionIfFails) { throw new IndexOutOfRangeException("Index " + index + " was >= than collection.Count " + count); } return; } var array = collection as Array; if (array != null) { try { ArrayExtensions.RemoveAt(ref array, index); } catch (Exception) { if (throwExceptionIfFails) { throw; } } collection = array; return; } var list = collection as IList; if (list != null) { try { list.RemoveAt(index); } catch (Exception) { if (throwExceptionIfFails) { throw; } } return; } var dictionary = collection as IDictionary; if (dictionary != null) { dictionary.RemoveAt(index); return; } var removeAtMethod = collection.GetType().GetMethod("RemoveAt"); if (removeAtMethod != null) { var parameters = removeAtMethod.GetParameters(); if (parameters.Length == 1) { var parameter = parameters[0]; if (parameter.ParameterType == Types.Int) { if (removeAtMethod.ReturnType == null || !typeof(ICollection).IsAssignableFrom(removeAtMethod.ReturnType)) //if(removeAtMethod.ReturnType == Types.Void || removeAtMethod.ReturnType == Types.Bool) { try { removeAtMethod.Invoke(collection, ArrayExtensions.TempObjectArray(index)); } catch (Exception) { if (throwExceptionIfFails) { throw; } } } else //if(typeof(ICollection).IsAssignableFrom(removeAtMethod.ReturnType)) { try { collection = (ICollection)removeAtMethod.Invoke(collection, ArrayExtensions.TempObjectArray(index)); } catch (Exception) { if (throwExceptionIfFails) { throw; } } } return; } } } if (throwExceptionIfFails) { throw new InvalidOperationException("Unable to remove item at index " + index + " from collection of type " + collection.GetType().Name + "."); } return; }