<p>How do I call the correct overloaded function given a reference to an object based on the actual type of the object. For example...</p>

<pre><code>class Test
{
    object o1 = new object();
    object o2 = new string("ABCD");
    MyToString(o1);
    MyToString(o2);//I want this to call the second overloaded function


    void MyToString(object o)
    {
        Console.WriteLine("MyToString(object) called.");
    }

    void MyToString(string str)
    {
        Console.WriteLine("MyToString(string) called.");
    }
}
</code></pre>

<p>what I mean is there a better option than the following?</p>

<pre><code>if(typeof(o) == typeof(string))
{
    MyToString((string)o);
}
else
{
    MyToString(o);
}
</code></pre>

<p>May be this can be done using reflection?</p>

Ok as soon as I hit post I remembered this can indeed be done using reflection...
```
```
var methInfo = typeof(Test).GetMethod("MyToString", new Type[] {o.GetType()});
methInfo.Invoke(this, new object[] {o});
```
```