title: C#- return IEnumerable<object> on task async call
body: <p>I have the following code:</p>
  
  <pre><code>public class TardiisServiceAsync
  {
      private static TardiisServiceAsync instance;
  
      public static TardiisServiceAsync Instance
      {
          //Singleton
          get
          {
              if (instance == null)
              {
                  instance = new TardiisServiceAsync();
              }
              return instance;
          }
      }
      public const string CACHE_PREFIX_TARDIIS_SERVICE = "TardiisServiceAsync_";
      public static Dictionary&lt;string, Tuple&lt;string, object&gt;&gt; clients = new Dictionary&lt;string, Tuple&lt;string, object&gt;&gt;();
      public delegate Task&lt;IEnumerable&lt;object&gt;&gt; GetServiceList(object client, string connectionId, TardiisServiceParameters parameters = null);
  
  
      public async Task&lt;IEnumerable&lt;object&gt;&gt; GetDemographicGroupsDelegateAsync(object client, string connectionId, TardiisServiceParameters parameters = null)
      {
          //Returns IEnumerable&lt;object&gt; task
          var test = Convert(client.GetType().GetMethod("GetDemographicGroupsAsync").Invoke(client, new object[] { connectionId }));
          var result = await test;
          return (IEnumerable&lt;object&gt;)result;
      }
  
      public async Task&lt;IdNameObject[]&gt; GetDemographicGroupsAsync()
      {
          //This task is called from hight level class
          Task&lt;IdNameObject[]&gt; cacheValue = null;
          string cacheKey = CACHE_PREFIX_TARDIIS_SERVICE + Membership.GetUser().UserName + "GetDemographicGroupsAsync" + GetMarketCode();
  
          if (ConfigurationManager.AppSettings[ConstantsHelper.FIELD_CONFIG_USE_CACHE_KEY] != null &amp;&amp; bool.Parse(ConfigurationManager.AppSettings[ConstantsHelper.FIELD_CONFIG_USE_CACHE_KEY]))
          {
              if (HttpContext.Current.Cache[cacheKey] == null)
              {
                  var taskResult = InitializeTardiisInstanceAndCallService(GetDemographicGroupsDelegateAsync);
                  var test = await taskResult;
                  cacheValue = (Task&lt;IdNameObject[]&gt;)HttpContext.Current.Cache[cacheKey];//This is not implemented yet
              } 
          }
  
          return await cacheValue;
      }
  
      public static Task&lt;T&gt; Convert&lt;T&gt;(T value)
      {
          return Task.FromResult&lt;T&gt;(value);
      }
  
      public static EnumHelper.EnumMarketCode GetMarketCode(EnumHelper.EnumMarketCode? marketCode = null)
      {
          //Returns market code
          if (!marketCode.HasValue)
              return (EnumHelper.EnumMarketCode.US);
          else
              return marketCode.Value;
      }
  
      private Task&lt;IEnumerable&lt;object&gt;&gt; InitializeTardiisInstanceAndCallService(GetServiceList getServiceList, TardiisServiceParameters parameters = null, EnumHelper.EnumMarketCode? marketCode = null, bool creatingConnectionsForAllCountries = false, string username = "")
      {
          //Get the instance and call the tardiis correct tardiis service depends on the market code
          marketCode = GetMarketCode(marketCode);
          MembershipUser membershipUser = Membership.GetUser();
          string currentUsername = (membershipUser != null ? membershipUser.UserName : username);
          string currentKey = currentUsername + "_" + marketCode;
  
          if (clients.ContainsKey(currentKey))
          {
              try
              {
                  //check if the connection is still active, or we should reconnect.
                  return getServiceList(clients[currentKey].Item2, clients[currentKey].Item1, parameters);
              }
              catch (Exception) { }
          }
          //At this point there is no connection, we need to open it.                            
          object client = TardiisServiceFactory.GetService(marketCode.Value);
  
          UserDTO userDTO = UserService.Instance.GetByUsername(currentUsername);
          TardiisUserDTO tardiisUserDTO = UserService.Instance.GetTardiisUserByUserIdAndMarketCode(userDTO.ID, marketCode.Value.ToString());
  
          if (tardiisUserDTO.TardiisUsername == null)
          {
              if (creatingConnectionsForAllCountries)
                  return null;
              else
                  throw new TardiisLoginException("Please enter your credentials");
          }
  
          object user = TardiisServiceFactory.GetUser(marketCode.Value, tardiisUserDTO.TardiisUsername, tardiisUserDTO.TardiisPassword);
          string connectionId;
          try
          {
              connectionId = (string)client.GetType().GetMethod("InitConnection").Invoke(client, new object[] { user });
          }
          catch (FaultException ex)
          {
              // This exception is catched globally by BaseController and prompts the user to enter his
              // tardiis credentials again
              throw new TardiisLoginException(ex.InnerException.Message, ex);
          }
          catch (TargetInvocationException ex)
          {
              // This exception is catched globally by BaseController and prompts the user to enter his
              // tardiis credentials again
              throw new TardiisLoginException(ex.InnerException.Message, ex);
          }
  
          clients[currentKey] = new Tuple&lt;string, object&gt;(connectionId, client);
          return getServiceList(clients[currentKey].Item2, clients[currentKey].Item1, parameters);
      }
  
      private void AddToCache(string key, object value)
      {
          HttpContext.Current.Cache.Add(key, value, null, DateTime.Now.AddMinutes(30), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
      }
  
  }
  </code></pre>
  
  <p>I am getting the following error message:</p>
  
  <p><strong>Unable to cast object of type 'System.Threading.Tasks.Task[System.Object] to type 'System.Collections.Generic.IEnumerable1[System.Object].</strong></p>
  
  <p>The result of the task is a <code>TypedClass[]</code>.</p>
  
  <p>How can I return as a <code>Task&lt;IEnumerable&lt;object&gt;&gt;</code> on the call method.</p>
  
  <p>This is a "copy" of a another class that have similar logic but in a synchronized way. The idea is change some methods 
  so they can be run synchronously.</p>
  
  <p>Thank you!</p>