I would like to create an extension method to be implemented like below (if it is even possible) which would be similar to the .ToString()
extension method. Can someone please point me in the right direction. I attempted to search it in Google and cannot locate anything.
DataTable table = db.GetInfo(UserId);
if (table.Rows.Count > 0)
{
bool b = table.Rows[0]["ColumnName"].MyExtensionMethod();
}
I would essentially like to simplify this:
bool b = Convert.IsDBNull(table.Rows[0]["ColumnName"]) ? false : bool.Parse(table.Rows[0]["ColumnName"].ToString());
to
bool b = table.Rows[0]["ColumnName"].DbBoolNullable();
You can do something like,
I think you'd be better off writing the extension method at the
DataRow
level, or even theDataTable
, instead ofobject
. First, you'd avoid pollutingobject
, and it would let you skip some ugly syntax.So instead of:
You could do the extension at the table and end up with:
Or do it at the row and end up with:
Since the indexer of DataRow returns a System.Object, your best bet is something like the following:
However, I'd strongly advise against this, as since this is an extension method for
System.Object
(which every class in .NET inherits from), then this method will apply to every variable.A better way would probably to make an extension method for
DataRow
:Then you could use it like so:
This:
table.Rows[0]["ColumnName"]
returns an Object.If you write an extension method for an object, every single class in .Net will get the extension. Try something else.
Take a look at my answer here: https://stackoverflow.com/a/5599559/467473 — a while back, I wrote a bunch of extension methods to do just the sort of thing you're wanting to do. To extend this technique, you'll be wanting to look at the documentation on type mapping between C# and SQL Server.
Further, if your using a recentish version of the .Net CLR, take a look at System.Data.DataRowExtensions, which might be just the thing that you're looking for.