Esempio n. 1
0
        /// <summary>
        /// Traces the computed property given by the trace expression.
        /// </summary>
        /// <param name="toTrace">The object to trace.</param>
        /// <param name="expression">The member expression.</param>
        /// <param name="traceMode">The trace mode to use.</param>
        /// <typeparam name="TObject">The type of the object.</typeparam>
        /// <typeparam name="TMember">The property or method of the object.</typeparam>
        /// <exception cref="InvalidOperationException">When the provided expression is not a member expression.</exception>
        public static void Trace <TObject, TMember>(this TObject toTrace, Expression <Func <TObject, TMember> > expression, TraceMode traceMode = TraceMode.Log)
        {
            if (expression is null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var observableObject = ObservableObject.GetFromObject(toTrace);

            if (observableObject == null)
            {
                throw new InvalidOperationException(Resources.CannotTraceNotObservable);
            }

            string key = expression.ExtractNameFromMemberExpression();

            var observableValue = observableObject[key];

            if (observableValue is IDerivation derivation)
            {
                derivation.Trace(traceMode);
                return;
            }

            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.ProvidedMemberNoDerivation, key));
        }
        /// <summary>
        /// Gets the underlying atom from the IReactiveObject instance.
        /// </summary>
        /// <param name="reactiveObject">The reactive object to get the atom for.</param>
        /// <returns>The IAtom instance to get.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when there is no atom provider internally.</exception>
        public static IObservable GetObservable(this object reactiveObject)
        {
            if (reactiveObject is null)
            {
                throw new ArgumentNullException(nameof(reactiveObject));
            }

            var observableObject = ObservableObject.GetFromObject(reactiveObject);

            if (observableObject != null)
            {
                return(((IAtomProvider)observableObject).Atom);
            }

            if (observableObject is IAtomProvider atomProvider)
            {
                return(atomProvider.Atom);
            }

            throw new ArgumentOutOfRangeException(nameof(reactiveObject));
        }
        /// <summary>
        /// Gets the observable for a member of an object.
        /// </summary>
        /// <typeparam name="TObject">The type of the object.</typeparam>
        /// <typeparam name="TMember">The type of the member.</typeparam>
        /// <param name="target">The target object to get a property from.</param>
        /// <param name="expression">The member expression.</param>
        /// <returns>An atom for the member of an object.</returns>
        public static IObservable GetObservable <TObject, TMember>(this TObject target, Expression <Func <TObject, TMember> > expression)
            where TObject : class
        {
            if (target is null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (expression is null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var observableObject = ObservableObject.GetFromObject(target);

            if (observableObject != null)
            {
                var name = expression.ExtractNameFromMemberExpression();

                if (!string.IsNullOrEmpty(name))
                {
                    if (observableObject.Has(name))
                    {
                        var value = observableObject[name];

                        return((IObservable)value);
                    }
                }
            }

            var ex     = expression.Compile();
            var result = ex(target);

            if (result is IAtomProvider atomProvider)
            {
                return(atomProvider.Atom);
            }

            throw new ArgumentOutOfRangeException(nameof(expression));
        }