public override object GetResults(IAsyncResult async)
            {
                object[]           state   = (object[])async.AsyncState;
                DataServiceRequest query   = (DataServiceRequest)state[0];
                DataServiceContext context = (DataServiceContext)state[1];

                Assert.IsTrue(async.IsCompleted);
                if (query is DataServiceQuery)
                {
                    return(UnitTestCodeGen.InvokeMethod(query.GetType(), "EndExecute", TypesIAsyncResult, null, query, new object[] { async }));
                }
                else
                {
                    return(UnitTestCodeGen.InvokeMethod(typeof(DataServiceContext), "EndExecute", TypesIAsyncResult, new Type[] { query.ElementType }, context, new object[] { async }));
                }
            }
        public static IEnumerable ExecuteQuery(DataServiceContext context, DataServiceRequest query, QueryMode queryMode)
        {
            bool isQuery = (null != (query as DataServiceQuery));

            object result = null;

            switch (queryMode)
            {
            case QueryMode.GetEnumerator:     // IEnumerable.GetEnumerator
            {
                if (isQuery)
                {
                    result = query;
                }
                else
                {
                    goto case QueryMode.ExecuteMethod;
                }
                break;
            }

            case QueryMode.ExecuteMethod:     // DataServiceQuery<T>.Execute
            {
                if (isQuery)
                {
                    result = UnitTestCodeGen.InvokeMethod(query.GetType(), "Execute", null, null, query, null);
                }
                else
                {
                    result = UnitTestCodeGen.InvokeMethod(typeof(DataServiceContext), "Execute", TypesUri, new Type[] { query.ElementType }, context, query.RequestUri);
                }

                break;
            }

            case QueryMode.AsyncExecute:     // DataServiceQuery<T>.BeginExecute and wait
            {
                if (isQuery)
                {
                    IAsyncResult async = (IAsyncResult)UnitTestCodeGen.InvokeMethod(query.GetType(), "BeginExecute", TypesAsyncCallbackObject, null, query, new object[] { null, null });
                    if (!async.CompletedSynchronously)
                    {
                        Assert.IsTrue(async.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, TestConstants.MaxTestTimeout), false), "BeginExecute timeout");
                    }

                    result = UnitTestCodeGen.InvokeMethod(query.GetType(), "EndExecute", TypesIAsyncResult, null, query, new object[] { async });
                }
                else
                {
                    IAsyncResult async = UnitTestCodeGen.InvokeMethod <DataServiceContext, IAsyncResult>("BeginExecute", TypesUriAsyncCallbackObject, new Type[] { query.ElementType }, context, query.RequestUri, null, null);
                    if (!async.CompletedSynchronously)
                    {
                        Assert.IsTrue(async.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, TestConstants.MaxTestTimeout), false), "BeginExecute timeout");
                    }

                    result = UnitTestCodeGen.InvokeMethod(typeof(DataServiceContext), "EndExecute", TypesIAsyncResult, new Type[] { query.ElementType }, context, async);
                }

                break;
            }

            case QueryMode.AsyncExecuteWithCallback:     // DataServiceQuery<T>.BeginExecute with callback
            {
                ExecuteCallback callback = new ExecuteCallback();
                IAsyncResult    async;
                if (isQuery)
                {
                    async = (IAsyncResult)UnitTestCodeGen.InvokeMethod(query.GetType(), "BeginExecute", TypesAsyncCallbackObject, null, query, new object[] { (AsyncCallback)callback.CallbackMethod, new object[] { query, context } });
                }
                else
                {
                    async = UnitTestCodeGen.InvokeMethod <DataServiceContext, IAsyncResult>("BeginExecute", TypesUriAsyncCallbackObject, new Type[] { query.ElementType }, context, new object[] { query.RequestUri, (AsyncCallback)callback.CallbackMethod, new object[] { query, context } });
                }

                Assert.IsTrue(callback.Finished.WaitOne(new TimeSpan(0, 0, TestConstants.MaxTestTimeout), false), "Asyncallback timeout");
                Assert.IsTrue(async.IsCompleted);

                if (null != callback.CallbackFailure)
                {
                    Assert.IsNull(callback.CallbackResult, callback.CallbackFailure.ToString());
                    throw new Exception("failure in callback", callback.CallbackFailure);
                }

                result = callback.CallbackResult;
                Assert.IsNotNull(result);
                break;
            }

            case QueryMode.BatchExecute:     // DataServiceContext.ExecuteBatch
            {
                LastUriRequest = query.RequestUri;
                int countBefore = context.Entities.Count + context.Links.Count;
                DataServiceResponse response = context.ExecuteBatch(query);
                int countAfter = context.Entities.Count + context.Links.Count;
                Assert.AreEqual(countBefore, countAfter, "should not materialize during ExecuteBatch");
                result = HandleQueryResponse(response, query, context);
            }
            break;

            case QueryMode.BatchAsyncExecute:     // DataServiceContext.BeginExecuteBatch and wait
            {
                int count = context.Entities.Count + context.Links.Count;
                LastUriRequest = query.RequestUri;
                IAsyncResult async = context.BeginExecuteBatch(null, null, query);
                if (!async.CompletedSynchronously)
                {
                    Assert.IsTrue(async.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, TestConstants.MaxTestTimeout), false), "BeginExecuteBatch timeout");
                }

                Assert.AreEqual(count, context.Entities.Count + context.Links.Count, "should not materialize until EndExecuteBatch");
                DataServiceResponse response = context.EndExecuteBatch(async);
                result = HandleQueryResponse(response, query, context);
                break;
            }

            case QueryMode.BatchAsyncExecuteWithCallback:     // DataServiceContext.BeginExecuteBatch with callback
            {
                ExecuteBatchCallback callback = new ExecuteBatchCallback();
                LastUriRequest = query.RequestUri;
                IAsyncResult async = context.BeginExecuteBatch(callback.CallbackMethod, new object[] { query, context }, query);

                Assert.IsTrue(callback.Finished.WaitOne(new TimeSpan(0, 0, TestConstants.MaxTestTimeout), false), "Asyncallback timeout {0}", LastUriRequest);
                Assert.IsTrue(async.IsCompleted);

                if (null != callback.CallbackFailure)
                {
                    Assert.IsNull(callback.CallbackResult, callback.CallbackFailure.ToString());
                    throw new Exception("failure in callback", callback.CallbackFailure);
                }

                result = callback.CallbackResult;
                Assert.IsNotNull(result);
                break;
            }

            default:
                Assert.Fail("shouldn't be here");
                break;
            }

            return((IEnumerable)result);
        }