C# Extension Method that reads an object as a specified type
<p>I find the following scenario quite often:
You have a string that you want to convert to an integer.</p>

<p>But first you must check:  </p>



<pre><code>        string sId = null;

        if (!string.IsNullOrEmpty(sId))
        {
            return int.Parse(sId);
        }
        else
        {
            return -1;
        }
</code></pre>

<p>But what i want is to be able to do this:</p>



<pre><code>sId.As&lt;int&gt;(-1)
</code></pre>

<p>So what i can do is write an exension method as follows:</p>



<pre><code>public static class ObjectExtensions
{
    public static T As&lt;T&gt;(this object instance, T defaultValue)
    {
        try
        {
            if (instance != null)
            {
                return (T)Convert.ChangeType(instance, typeof(T));
            }
        }
        catch
        {                
        }
        return defaultValue;
    }
}
</code></pre>

<p>My question is does anyone have a suggestion that this is a good approach?</p>

<p>Is there anything built into .NET 3.5 or up that is built in that can do this?</p>
