在Mono 2.10 REPL中使用:
csharp> string a = "true";
csharp> a.ToBoolean(CultureInfo.InvariantCulture);
{interactive}(1,4): error CS1061: Type `string' does not contain a definition fo
r `ToBoolean' and no extension method `ToBoolean' of type `string' could be foun
d (are you missing a using directive or an assembly reference?)
C:\PROGRA~1\MONO-2~1.2\lib\mono\4.0\mscorlib.dll (Location of the symbol related
to previous error)
csharp> ((IConvertible)a).ToBoolean(CultureInfo.InvariantCulture);
true
csharp>根据文档,System.String实现了IConvertible。如果这是真的,为什么
a.ToBoolean(CultureInfo.InvariantCulture);失败?
为什么我必须将它转换为IConvertible才能使ToBoolean工作?
发布于 2011-08-27 06:27:15
正如the documentation for ToBoolan() for string on MSDN所说:
此成员为an explicit interface member implementation。只有在将
String实例强制转换为IConvertible接口时,才能使用它。推荐的替代方法是调用Convert.ToBoolean(String)方法。
发布于 2011-08-27 06:26:27
该接口是显式实现的。
https://stackoverflow.com/questions/7211114
复制相似问题