C# 中的自动类型转换
我知道您可以重写对象的 ToString() 方法,以便每次调用对象或将其传递给需要 String 类型的函数时,它都会转换为 String。
我已经为对象类型“object”编写了几个扩展方法,
public static DateTime ToDate(this object date)
{
return DateTime.Parse(date.ToString());
}
public static int ToInteger(this object num)
{
return Int32.Parse(num.ToString());
}
public static long ToLong(this object num)
{
return Int64.Parse(num.ToString());
}
以便我可以像这样调用它们:
eventObject.Cost = row["cost"].ToString();
eventObject.EventId = row["event_id"].ToLong();
但是,我想要完成的是将“object”类型的行对象根据属性转换为正确的类型在我的“eventObject”上键入。因此,我可以这样称呼它:
eventObject.Cost = row["cost"];
eventObject.EventId = row["event_id"];
如果重要的话,该行是一个 DataRow。
I know that you could override an object's ToString() Method, so that everytime you call an object or pass it to a function that requires a String type it will be converted to a String.
I have written several extension methods for object type 'object'
public static DateTime ToDate(this object date)
{
return DateTime.Parse(date.ToString());
}
public static int ToInteger(this object num)
{
return Int32.Parse(num.ToString());
}
public static long ToLong(this object num)
{
return Int64.Parse(num.ToString());
}
so that I could just call them like this:
eventObject.Cost = row["cost"].ToString();
eventObject.EventId = row["event_id"].ToLong();
However, what I want to accomplish is to convert the row objects which is of type 'object' to its correct type based on the property types on my 'eventObject'. So, I could call it like this:
eventObject.Cost = row["cost"];
eventObject.EventId = row["event_id"];
The row is a DataRow if that matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
C# 支持隐式类型转换,您可以将其用于自定义类型,如下所示:
并支持 扩展方法,但不支持隐式转换作为扩展方法,如下所示:
C# supports implicit conversion for types and you can use it for your custom types like the following:
And supports extension methods, but doesn't support implicit conversion as an extension method like the following:
不,你错了。以下代码将无法编译:
编译器不会先获取
MyMethod
参数的类型(即string
),然后尝试转换您传递的参数(其类型为MyClass
) 到它。我想您可能会被Console.WriteLine
之类的东西误导。基于上面的代码,
将“MyClass”打印到控制台,编译器似乎知道您应该将字符串传递给Console.WriteLine(new MyClass())
Console.WriteLine
并尝试将MyClass
转换为字符串。但最重要的是Console.WriteLine
有几个重载,其中之一是针对object
的:No, you are wrong. The following code won't compile:
The compiler will not get the type of
MyMethod
parameter(which isstring
) first and try to convert the argument you passed(whose type isMyClass
) to it. I guess you are probably mislead by something likeConsole.WriteLine
. Base on the code above,
prints "MyClass" to the console, it seems that the compiler knows you should pass a string toConsole.WriteLine(new MyClass())
Console.WriteLine
and try to convertMyClass
to string. But the essential isConsole.WriteLine
has several overloads, one of them is forobject
:我相信您正在寻找的是隐式转换,如下所述: http: //msdn.microsoft.com/en-us/library/z5z9kes2.aspx。
但是,由于我链接到的页面上概述的原因,将这些添加到对象将是一个非常糟糕的主意。
I believe what you're looking for is implicit conversion, which is described here: http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx.
However, adding these to object would be a very bad idea, for reasons outlined on the page I've linked to.
如果我说的是显而易见的事情,请原谅我,但我在读完你的问题后得到了这个印象。
您知道 .NET 中每种类型的基本类型都是对象吗?
因此,您描述的类型对象的数据行中的列也可以与您尝试分配给的成员具有相同的类型,然后需要进行简单的转换。
例如
eventObject.EventId = (int)row["event_id"];
Forgive me if I'm stating the obvious but I got this impression after reading your question.
You know the basetype of every type in .NET is object right?
So the column in the datarow which you describe of type object could also just as well be of the same type as the member you're trying to assign to and then a simple cast is needed.
for instance
eventObject.EventId = (int)row["event_id"];
总结一下 - 只需定义这些方法,您就可以在需要 String 作为参数的方法中使用 Test 类的对象。
To sum up - just define these methods and you can use objects of class Test in methods requiring String as a parameter.