DataRow 的通用字段 getter
我尝试使用此通用方法扩展 DataRow 对象:
public static T? Get<T>(this DataRow row, string field) where T : struct
{
if (row.IsNull(field))
return default(T);
else
return (T)row[field];
}
当 T 为 int
、decimal
、double
等时,它工作正常。
但是当我尝试与字符串一起使用,我有这个错误:
“‘字符串’类型必须是 不可为 null 的值类型,以便 将其用作泛型中的参数“T” 类型或方法“System.Nullable””
如何更正此问题?
我知道字符串不是结构,但如果字符串字段为 DBNull,我不想返回 null。
I try to extend the DataRow object with this generic method :
public static T? Get<T>(this DataRow row, string field) where T : struct
{
if (row.IsNull(field))
return default(T);
else
return (T)row[field];
}
It's work fine when T is int
, decimal
, double
, etc.
But when I try to use with string, I have this error :
"The type 'string' must be a
non-nullable value type in order to
use it as parameter 'T' in the generic
type or method 'System.Nullable'"
How can I correct this ?
I know that string is not a struct but I wan't to return null if the string field is DBNull.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为这就是你想要的:
I think that this is what you want:
string
不是一个struct
,而是一个class
。这就是错误消息告诉您的内容。只需删除约束即可。也许您想查看DataRowExtensions。
string
is not astruct
, but aclass
. That is what the error message tells you. Just remove the constraint.Maybe you want to have a look at the DataRowExtensions.
您有一个明确的条件阻止其使用字符串:
System.String< /a> 是一个类,而不是一个结构。如果您的目标是处理值类型和字符串,我将为字符串创建一个单独的方法,并将其留给其他类型。
You have an explicit condition that prevents this from working with string:
System.String is a class, not a struct. If your goal is to handle value types and string, I would make a separate method for string, and leave this alone for your other types.
不幸的是,您将无法通过使用泛型获得 Nullable 返回类型和对引用类型的支持,除非您指定希望在进行调用时返回 Nullable
,并且当您调用时
我没有对此进行测试,只是把它扔在这里。试一试。
编辑:
或者,如果您确实想将值类型转换为可空值并且仍然能够支持引用类型,类似这样的东西可能会起作用
但是,它需要对每次使用进行强制转换
,但是,这不会像这样与仅接受泛型类型参数中的可为空类型一样高效。
Unfortunately, you're not going to be able to get the Nullable return type AND support for reference types by using generics, unless you specify that you want a Nullable returned when you make the call
and when you call
I didn't test this, just tossed it up here. Give it a shot.
EDIT:
Alternatively, if you really wanted to convert the value types into nullables and still be able to support reference types something like this might work
However, it'd require a cast on every usage
This is, however, not going to be nearly as efficient as just accepting the nullable type in the generic type parameters.
正如韦斯指出的,你的问题是结构的约束。我希望扩展方法能够在没有约束的情况下工作...
啊,我现在明白了,您正在返回
T?
好吧,我不确定,但是您可以定义一个约束方法的两个变体吗到struct
,另一个到class
并返回T
?As Wes points out, your issue is the constraint to struct. I'd expect that extension method to work without constraints...
Ah, I see now, you're returning
T?
Well, I am not sure but can you define two variants of the method one constraining tostruct
, the other toclass
and returningT
?像这样的事情怎么样?与您的示例不太一样,但对于引用类型、可为空值类型和不可为空值类型非常有用:
How about something like this? Not quite the same as your example, but pretty usable for reference-types, nullable value-types and non-nullable value-types alike: