Exemplo n.º 1
0
        /// <remarks>
        /// TODO: need to find a better way to invoke instance method and pass paramethers
        /// </remarks>
        public static T InvokeOverridden <T>(this IUIContext context, Func <T> defaultImplementation, params object[] parameters)
        {
            StackTrace stackTrace = new StackTrace();
            var        caller     = stackTrace.GetFrame(1).GetMethod();
            var        overridden = context.GetType().GetMethod(caller.Name);

            if (overridden != null && VerifyMethodSignature(caller, overridden))
            {
                return((T)overridden.Invoke(context, parameters));
            }
            else
            {
                return(defaultImplementation.Invoke());
            }
        }
        public static IUISelector <T> createSelector <T>(IUIContext context, string name, Action <T> updateFunc = null
#if UNITY_EDITOR && UI_SELECTOR_STATS
                                                         , [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
                                                         [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber  = 0
#endif
                                                         )
        {
            if (!context.ContainsProperty(name))
            {
                throw new ArgumentException($"Property {name} not registered in context {context.GetType().FullName}");
            }

            object target;

            if (context.target.TryGetTarget(out target) && context.implementsInterfaces != null)
            {
                foreach (var implementInterface in context.implementsInterfaces)
                {
                    if (implementInterface.GetProperty(name) != null && !implementInterface.IsGenericType && !implementInterface.IsAssignableFrom(target.GetType()))
                    {
                        Debug.LogWarning("The data type of " + target.GetType() + " does not implement the interface " + implementInterface.Name);
                    }
                }
            }

            Func <T> propertyGetter = () =>
            {
                object getterTarget;
                if (!context.target.TryGetTarget(out getterTarget))
                {
                    Debug.LogError("Cannot get the Target Reference");
                }

                if (PropertyContainer.TryGetValue <T>(ref getterTarget, name, out var value))
                {
                    return(value);
                }
                else
                {
                    return(default);