C# C语言中的自动类型转换#
我知道可以重写对象的ToString()方法,这样每次调用对象或将其传递给需要字符串类型的函数时,它都将转换为字符串 我已经为对象类型“object”编写了几个扩展方法C# C语言中的自动类型转换#,c#,.net,casting,type-conversion,overriding,C#,.net,Casting,Type Conversion,Overriding,我知道可以重写对象的ToString()方法,这样每次调用对象或将其传递给需要字符串类型的函数时,它都将转换为字符串 我已经为对象类型“object”编写了几个扩展方法 public static DateTime ToDate(this object date) { return DateTime.Parse(date.ToString()); } public static int ToInteger(this object num) { return Int32.Pars
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();
eventObject.Cost = row["cost"];
eventObject.EventId = row["event_id"];
但是,我想要完成的是根据我的“eventObject”上的属性类型将类型为“object”的行对象转换为正确的类型。所以,我可以这样称呼它:
eventObject.Cost = row["cost"].ToString();
eventObject.EventId = row["event_id"].ToLong();
eventObject.Cost = row["cost"];
eventObject.EventId = row["event_id"];
如果这很重要的话,该行就是DataRow。我相信您正在寻找的是隐式转换,如下所述: 但是,将这些添加到对象中是一个非常糟糕的主意,原因在我链接到的页面上有概述。C#支持类型转换,您可以将其用于自定义类型,如下所示:
class CustomValue
{
public static implicit operator int(CustomValue v) { return 4; }
public static implicit operator float(CustomValue v) { return 4.6f; }
}
class Program
{
static void Main(string[] args)
{
int x = new CustomValue(); // implicit conversion
float xx = new CustomValue(); // implicit conversion
}
}
static class MyExtension
{
// Not supported
public static implicit operator bool(this CustomValue v)
{
return false;
}
}
和支持,但不支持作为扩展方法的隐式转换,如下所示:
class CustomValue
{
public static implicit operator int(CustomValue v) { return 4; }
public static implicit operator float(CustomValue v) { return 4.6f; }
}
class Program
{
static void Main(string[] args)
{
int x = new CustomValue(); // implicit conversion
float xx = new CustomValue(); // implicit conversion
}
}
static class MyExtension
{
// Not supported
public static implicit operator bool(this CustomValue v)
{
return false;
}
}
我知道你可以推翻
对象的ToString()方法,以便
每次调用对象或传递时
它是一个需要
字符串类型它将被转换为
绳子
不,你错了。无法编译以下代码:
class MyClass
{
public override string ToString()
{
return "MyClass";
}
}
static void MyMethod(string s) { }
static void Main(string[] args)
{
MyMethod(new MyClass()); //compile error
}
编译器不会首先获取MyMethod
参数的类型(即string
)并尝试将您传递的参数(其类型为MyClass
)转换为它。我猜你可能被类似于Console.WriteLine
的东西误导了。基于上述代码,
Console.WriteLine(new MyClass())
将“MyClass”打印到控制台,编译器似乎知道应该将字符串传递到Console.WriteLine
,并尝试将MyClass
转换为字符串。但是最重要的是控制台。WriteLine
有几个重载,其中一个重载用于对象
:
//from Console.cs
public static void WriteLine(object value)
{
//in this method there is something like
//Console.WriteLine(value.ToString());
}
如果我说的是显而易见的,请原谅,但我是在读了你的问题后得到这个印象的 您知道.NET中每种类型的基类型都是object,对吗? 因此,您描述的类型为object的datarow中的列也可以与您试图分配给的成员的类型相同,然后需要一个简单的强制转换 比如说
eventObject.EventId=(int)行[“event_id”] 总之,只要定义这些方法,就可以在需要字符串作为参数的方法中使用类Test的对象
class Test
{
private String txt;
public static implicit operator string(Test t)
{
return t.ToString();
}
public override string ToString()
{
return txt;
}
}
为什么不使用Convert类呢不,除非您让“row[x]”返回一个知道如何进行隐式强制转换的自定义对象,否则您不能像上一个代码那样进行静默类型转换。+1@Lasse使用Convert类比使用实用方法打包
对象更可取。现在将使用,我认为它没有任何区别。我可以为System.Data.DataRow添加隐式运算符吗?不,你不能,因为它只提供用户定义的类型。你可以组合它,它是一个ToCustomValue
扩展方法