我想扩展System.Data.DataRow类,所以我编写了以下代码(并在类中添加了using关键字)
namespace MyExtension
{
public static class DataRowExt
{
public static string ToNullableDate(this DataRow dr)
{ return something;}
}
}但是,当我尝试以下代码时,ToNullableDate没有出现在智能感知中:
DataRow d = new DataRow();
d["Column"].ToNullableDate(); // ToNullableDate does not show up我的扩展方法语句中是否遗漏了什么?或者我应该只创建一个新类并从DataRow类继承?
谢谢
发布于 2012-07-09 08:09:19
扩展方法应该是off,d,而不是关闭索引器。
仅供参考,您可以这样做:
DataRow d = new DataRow();
d["Column"] as DateTime?;发布于 2012-07-09 08:11:39
你的代码里有using MyExtension吗?此外,扩展名在DataRow上,它是d,但您要在d"Column“中查找它。
从你的评论来看,这个值似乎有null,你需要测试一下,而不是创建一个扩展……
object value = d["Column"];
if (value == DBNull.Value)
// do something
else
// do something else 如果你真的想要一个扩展方法,一种选择是在方法中传递列:
namespace MyExtension
{
public static class DataRowExt
{
public static string ToNullableDate(this DataRow dr, string Column)
{ return something;}
}
}然后像这样使用它:
d.ToNullableDate("Column");https://stackoverflow.com/questions/11387746
复制相似问题