예제 #1
0
            private void Initialize(Action continuation, IAsyncStateMachine stateMachine, IAsyncOperation asyncOperation)
            {
                const MemberFlags flags = MemberFlags.NonPublic | MemberFlags.Public | MemberFlags.Instance;

                if (stateMachine == null)
                {
                    stateMachine = continuation.Target as IAsyncStateMachine;
                    if (stateMachine == null)
                    {
                        var fieldInfo = continuation.Target
                                        .GetType()
                                        .GetFieldEx("m_continuation", flags);
                        if (fieldInfo != null)
                        {
                            continuation = fieldInfo.GetValueEx <Action>(continuation.Target);
                        }

                        fieldInfo = continuation.Target
                                    .GetType()
                                    .GetFieldEx("m_stateMachine", flags);
                        if (fieldInfo == null)
                        {
                            TraceError(continuation.Target);
                            return;
                        }
                        stateMachine = fieldInfo.GetValueEx <IAsyncStateMachine>(continuation.Target);
                        if (stateMachine == null)
                        {
                            TraceError(continuation.Target);
                            return;
                        }
                    }
                }
                var type = stateMachine.GetType();

                StateMachineType = type.AssemblyQualifiedName;
                FieldSnapshots   = new List <FieldSnapshot>();

                foreach (var field in type.GetFieldsEx(flags))
                {
                    var snapshot = FieldSnapshot.Create(field, stateMachine, asyncOperation);
                    if (snapshot != null)
                    {
                        FieldSnapshots.Add(snapshot);
                    }
                }
            }
예제 #2
0
        public ISerializableCallback CreateSerializableCallback(Delegate @delegate)
        {
            Should.NotBeNull(@delegate, "delegate");
            var  method = @delegate.GetMethodInfo();
            bool firstParameterSource;

            if (!CheckMethodParameters(method, out firstParameterSource))
            {
                Tracer.Warn("The method '{0}' cannot be serialized, invalid parameters.", method);
                return(null);
            }
            if (method.IsStatic)
            {
                return(new DelegateSerializableCallback(method.DeclaringType.AssemblyQualifiedName, method.Name,
                                                        firstParameterSource, true, null, null));
            }

            var target     = @delegate.Target;
            var targetType = target.GetType();

            if (targetType.IsAnonymousClass())
            {
                var snapshots = new List <FieldSnapshot>();
                foreach (var anonymousField in targetType.GetFieldsEx(MemberFlags.Instance | MemberFlags.NonPublic | MemberFlags.Public))
                {
                    var snapshot = FieldSnapshot.Create(anonymousField, target, null);
                    if (snapshot != null)
                    {
                        snapshots.Add(snapshot);
                    }
                }
                return(new DelegateSerializableCallback(targetType.AssemblyQualifiedName, method.Name, firstParameterSource,
                                                        false, null, snapshots));
            }
            return(new DelegateSerializableCallback(targetType.AssemblyQualifiedName, method.Name, firstParameterSource,
                                                    false, targetType.IsSerializable() ? target : null, null));
        }