Search code examples
c#functionargstrello

How do I pass a string to a function requiring an Object?


I am using Chello (the c# wrapper for the Trello API). I need to pass the argument "createCard" as per the documentation here: https://trello.com/docs/api/card/index.html

And this is the function I am using from Chello:

public IEnumerable<CardUpdateAction> ForCard(string cardId, object args)
    {
        string queryString = BuildQueryString(args);

        return GetRequest<List<CardUpdateAction>>("/cards/{0}/actions?{1}", cardId, queryString);
    }

I have tried calling this in this way:

 List<CardUpdateAction> cua = chello.CardUpdates.ForCard("5264d37736695b2821001d7a","createCard").ToList();

but I get the error: Parameter Count Mismatch

on this function:

 protected static string BuildQueryString(object args)
    {
        string queryString = String.Empty;
        if (args != null)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var prop in args.GetType().GetProperties())
            {
                sb.AppendFormat("{0}={1}&", prop.Name, prop.GetValue(args, null));
            }
            if (sb.Length > 0) sb.Remove(sb.Length - 1, 1);
            queryString = sb.ToString();
        }
        return queryString;
    }

Solution

  • The problem is the fact that your API you are using expects you to pass in a class that has public properties equal to the tags you want to use.

    This is very easy to do using Anonymous Types (I am doing a slightly different example to help illustrate a point)

    //This will cause BuildQueryString to return "actions=createCard&action_fields=data,type,date"
    var options = new { actions = "createCard", action_fields = "data,type,date" };
    
    List<CardUpdateAction> cua = chello.CardUpdates.ForCard("5264d37736695b2821001d7a",options).ToList();