Beispiel #1
0
        void IWait.Post <D>(D item)
        {
            this.ValidateNeed(Need.Wait);

            var post = this as IPost <D>;

            if (post == null)
            {
                if (typeof(D).IsValueType)
                {
                    var postBoxed = this as IPost <object>;
                    if (postBoxed != null)
                    {
                        post = new PostStruct <D>(postBoxed);
                    }
                }
            }

            if (post == null)
            {
                IWait wait = this;
                wait.Fail(new InvalidTypeException(this, typeof(D)));
            }
            else
            {
                post.Post(item);
            }
        }
Beispiel #2
0
        void IWait.Post <D>(D item)
        {
            this.ValidateNeed(Need.Wait);

            // try generic type variance first
            var post = this as IPost <D>;

            if (post == null)
            {
                // then work around lack of generic type variant for value types
                if (typeof(D).IsValueType)
                {
                    var postBoxed = this as IPost <object>;
                    if (postBoxed != null)
                    {
                        post = new PostStruct <D>(postBoxed);
                    }
                }
            }

            if (post != null)
            {
                post.Post(item);
            }
            else
            {
                // if we have runtime type information, use reflection and recurse
                var  type       = item?.GetType();
                bool reflection = type != null && !type.IsAssignableFrom(typeof(D));
                if (reflection)
                {
                    var generic = MethodPost.MakeGenericMethod(type);
                    generic.Invoke(this, new object[] { item });
                }
                else
                {
                    // otherwise, we cannot satisfy this wait with this item
                    IWait wait = this;
                    wait.Fail(new InvalidTypeException(this, typeof(D)));
                }
            }
        }