Example #1
0
 /// <summary>Raises the <see cref="BeginInvocation"/> event.</summary>
 /// <param name="e">The <see cref="InvocationEventArgs"/> instance containing the event data.</param>
 /// <autogeneratedoc />
 /// TODO Edit XML Comment Template for OnBeginInvocation
 // ReSharper disable once UnusedMember.Global
 protected virtual void OnBeginInvocation(InvocationEventArgs e)
 {
     BeginInvocation?.Invoke(this, e);
 }
Example #2
0
        /// <summary>Intercepts the specified invocation.</summary>
        /// <param name="invocation">The invocation.</param>
        /// <autogeneratedoc />
        /// TODO Edit XML Comment Template for Intercept
        public override void Intercept(IInvocation invocation)
        {
            Interlocked.Increment(ref _callDepth);
            var x   = _stack.Any( ) ? _stack.Peek( ) : null;
            var myX = new StackInfo( );

            _stack.Push(myX);
            var writeLine = WriteLine;

            if (x != null)
            {
                x.Written = true;
                writeLine("");
            }


            var formatted = FormatInvocation(invocation, "");
            var a         = new InvocationEventArgs {
                Formatted = formatted
            };

            DumpInvocation(invocation, _callDepth);
            invocation.Proceed( );
            try
            {
                DumpReturnValue(invocation, _callDepth, !myX.Written);
                if (ProxyGenerator != null &&
                    invocation.ReturnValue != null &&
                    !invocation.ReturnValue.GetType( ).IsSealed &&
                    !(invocation.ReturnValue is IProxyTargetAccessor))
                {
                    var r = invocation.ReturnValue;
                    if (r is XamlType typ)
                    {
                        var invoker =
                            ( XamlTypeInvoker )ProxyGenerator.CreateClassProxyWithTarget(
                                typeof(
                                    XamlTypeInvoker
                                    )
                                , typ
                                .Invoker
                                , new[]
                        {
                            r
                        }
                                , this
                                );
                        object[] args = { typ.UnderlyingType, typ.SchemaContext, invoker };


                        invocation.ReturnValue =
                            ProxyGenerator.CreateClassProxyWithTarget(
                                r.GetType( )
                                , r
                                , args
                                , this
                                );
                    }
                    else if (r.GetType( ).IsGenericType &&
                             r.GetType( ).GetGenericTypeDefinition( )
                             == typeof(XamlValueConverter <object>)
                             .GetGenericTypeDefinition( ))
                    {
                        object[] args = null;
                        switch (r)
                        {
                        case XamlValueConverter <ValueSerializer> q: args = new object[] { q.ConverterType, q.TargetType, q.Name };
                            break;

                        case XamlValueConverter <TypeConverter> z:   args = new object[] { z.ConverterType, z.TargetType, z.Name };
                            break;
                        }

                        if (args != null)
                        {
                            invocation.ReturnValue =
                                ProxyGenerator.CreateClassProxyWithTarget(
                                    r.GetType( )
                                    , r
                                    , args
                                    , this
                                    );
                        }
                    }
                    else if (r.GetType( ).IsGenericType &&
                             r.GetType( ).GetGenericTypeDefinition( )
                             == typeof(ReadOnlyCollection <object>)
                             .GetGenericTypeDefinition( ))
                    {
                        var propInfo = r.GetType( )
                                       .GetField(
                            "list"
                            , BindingFlags.NonPublic | BindingFlags.Instance
                            );
                        if (propInfo != null)
                        {
                            var args = new[] { propInfo.GetValue(r) };
                            invocation.ReturnValue =
                                ProxyGenerator.CreateClassProxyWithTarget(
                                    r.GetType( )
                                    , r
                                    , args
                                    , this
                                    );
                        }
                    }
                    else
                    {
                        switch (r)
                        {
                        case XamlDirective d:
                        {
                            object[] args =
                            {
                                d.GetXamlNamespaces( ), d.Name, d.Type, d.TypeConverter
                                ,                       d.AllowedLocation
                            };

                            invocation.ReturnValue =
                                ProxyGenerator.CreateClassProxyWithTarget(
                                    r.GetType( )
                                    , r
                                    , args
                                    , this
                                    );
                            break;
                        }

                        case NamespaceDeclaration ns:
                            invocation.ReturnValue =
                                ProxyGenerator.CreateClassProxyWithTarget(
                                    r.GetType( )
                                    , r
                                    , new object[]
                            {
                                ns.Namespace, ns.Prefix
                            }
                                    , this
                                    );
                            break;

                        default:
                            try
                            {
                                invocation.ReturnValue =
                                    ProxyGenerator.CreateClassProxyWithTarget(
                                        r.GetType( )
                                        , r
                                        , this
                                        );
                            }
                            catch (InvalidProxyConstructorArgumentsException)
                            {
                                writeLine("Constructors for ⮜" + FormatTyp(r.GetType( )) + "⮞");
                                foreach (var constructorInfo in r.GetType( ).GetConstructors( ))
                                {
                                    writeLine(
                                        FormatTyp(constructorInfo.DeclaringType)
                                        + " ( "
                                        + string.Join(
                                            " , "
                                            , constructorInfo
                                            .GetParameters( )
                                            .Select(
                                                (info, i)
                                                => FormatTyp(
                                                    info
                                                    .ParameterType
                                                    )
                                                + " "
                                                + info.Name
                                                + (info.HasDefaultValue
                                                                                         ? " = "
                                                   + info
                                                   .DefaultValue
                                                                                         : "")
                                                )
                                            )
                                        );
                                }
                            }

                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                writeLine("--Exception--");
                writeLine(ex.ToString( ));      //.GetType ( ) ) ;
                //Console.WriteLine ( ex.Message ) ;
                writeLine("--End Exception--");
            }
            finally
            {
                _stack.Pop( );
                Interlocked.Decrement(ref _callDepth);
            }
        }