C#:使用泛型将对象转换为无符号数字类型
我正在尝试编写一些代码来将数据从对象类型字段(来自数据集)转换为其目标(类型化)字段。 我正在使用(至少尝试)它 动态转换。 它似乎适用于字符串、整数、日期时间。
但它不适用于无符号类型(ulong、uint)。 下面有一个简单的代码,显示了我想要做的事情。 如果将 ul var 类型从 ulong 更改为 int,则可以正常工作。
有人知道吗?
public class console
{
public static void CastIt<T>(object value, out T target)
{
target = (T) value;
}
public static void Main()
{
ulong ul;
string str;
int i;
DateTime dt;
object ul_o = (object) 2;
object str_o = (object) "This is a string";
object i_o = (object)1;
object dt_o = (object) DateTime.Now;
Console.WriteLine("Cast");
CastIt(ul_o, out ul);
CastIt(str_o, out str);
CastIt(i_o, out i);
CastIt(dt_o, out dt);
Console.WriteLine(ul);
Console.WriteLine(str);
Console.WriteLine(i);
Console.WriteLine(dt.ToString());
}
}
I'm trying to write some code to convert data from a object type field (come from a DataSet) into it's destination (typed) fields. I'm doing (trying at least) it using
dynamic conversion. It seems to work fine for strings, int, DateTime.
But it doesn't work for unsigned types (ulong, uint). Below there's a simple code that shows what I want to do. If you change the ul var type from ulong to int, it works fine.
Does anybody have a clue?
public class console
{
public static void CastIt<T>(object value, out T target)
{
target = (T) value;
}
public static void Main()
{
ulong ul;
string str;
int i;
DateTime dt;
object ul_o = (object) 2;
object str_o = (object) "This is a string";
object i_o = (object)1;
object dt_o = (object) DateTime.Now;
Console.WriteLine("Cast");
CastIt(ul_o, out ul);
CastIt(str_o, out str);
CastIt(i_o, out i);
CastIt(dt_o, out dt);
Console.WriteLine(ul);
Console.WriteLine(str);
Console.WriteLine(i);
Console.WriteLine(dt.ToString());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如 Andrew 所说,问题在于您无法将装箱的
int
拆箱为ulong
。两个选项:
1) 改为使用
ulong
:或者
2) 让
CastIt
使用Convert.ChangeType
:这有点臭,但适用于您的示例代码。 如果你能在实际代码中使用第一种方式,那就更好了。
As Andrew says, the problem is that you can't unbox from a boxed
int
toulong
.Two options:
1) Box a
ulong
instead:or
2) Make
CastIt<T>
useConvert.ChangeType
:This is a bit smelly, but works with your example code. If you can use the first way in your real code, that would be better.
那是因为你的 ul_o 对象是一个 int,而不是一个无符号数。 当您使用转换函数时,您会在
对象
的上下文中拥有目标数据的同时进行转换。 显式/隐式强制转换运算符(这是您需要使用的)仅当您在实现它们的类型的上下文中拥有对象时才起作用(因为这些运算符在编译时静态链接,而不是在运行时动态链接)。如果这确实是您想要做的,而不仅仅是直接转换,请使用以下命令:
That's because your ul_o object is an int, not an unsigned number. When you're in your casting function, you're casting while having the target data in the context of an
object
. Explicit/implicit cast operators (which is what you'd need to be using) only work when you have the object in the context of a type that implements them (since those operators are statically linked at compile time rather than dynamically at runtime).If this is really what you want to do, instead of just a straight cast, use this:
CLR 不允许您以这种方式进行转换,因为您的装箱值类型实际上是 int:
当您尝试转换为
ulong
时,您不能这样做,因为您无法取消装箱int 直接转换为
ulong
。The CLR does not allow you to cast this way because your boxed value type is in fact an int:
When you are attempting to cast to a
ulong
you cannot because you cannot unbox anint
directly into aulong
.我认为你想要的更像是(未经测试,但方向正确)......
编辑:由 Skeet 独家报道! :)
I think what you want is more like (untested, but directionally correct) ...
Edit: Scooped by Skeet! :)
这并不是你问题的真正答案; 只是想提一下,如果您使用 .Net 3.5,则 Linq to DataSets 代码包含与您正在实现的功能类似的功能。 具体的扩展方法将是DataRow类上的Field()。
This is not really an answer to your question; Just wanted to mention that if you are using .Net 3.5, the Linq to DataSets code includes functionality like what you are implementing. The specific extension method would be Field<T>() on the DataRow class.