0

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;
    }
Ben
  • 4,281
  • 8
  • 62
  • 103

2 Answers2

3

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();
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
1

string is an object. Every type in .NET platform inherits from Object. This is called Unified Type System.

On the other hand, we have the Liskov Substitution Principle, which put simply, says that if B is a subtype of A (B is A), then you should be able to use B, wherever A is used.

Based on these reasons, you can pass string to any method that accepts an object as an argument.

You can test it:

public void DoSomething(object args)
{
}

public void Main()
{
    DoSomething("some string argument, instead of the object");
}

It works just fine. No error.

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188