本文整理汇总了C#中System.InvalidCastException类的典型用法代码示例。如果您正苦于以下问题:C# InvalidCastException类的具体用法?C# InvalidCastException怎么用?C# InvalidCastException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
InvalidCastException类属于System命名空间,在下文中一共展示了InvalidCastException类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
bool flag = true;
try {
IConvertible conv = flag;
Char ch = conv.ToChar(null);
Console.WriteLine("Conversion succeeded.");
}
catch (InvalidCastException) {
Console.WriteLine("Cannot convert a Boolean to a Char.");
}
try {
Char ch = Convert.ToChar(flag);
Console.WriteLine("Conversion succeeded.");
}
catch (InvalidCastException) {
Console.WriteLine("Cannot convert a Boolean to a Char.");
}
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:26,代码来源:InvalidCastException 输出:
Cannot convert a Boolean to a Char.
Cannot convert a Boolean to a Char.
示例2: Main
//引入命名空间
using System;
public class Person
{
String _name;
public String Name
{
get { return _name; }
set { _name = value; }
}
}
public class PersonWithId : Person
{
String _id;
public string Id
{
get { return _id; }
set { _id = value; }
}
}
public class Example
{
public static void Main()
{
Person p = new Person();
p.Name = "John";
try {
PersonWithId pid = (PersonWithId) p;
Console.WriteLine("Conversion succeeded.");
}
catch (InvalidCastException) {
Console.WriteLine("Conversion failed.");
}
PersonWithId pid1 = new PersonWithId();
pid1.Name = "John";
pid1.Id = "246";
Person p1 = pid1;
try {
PersonWithId pid1a = (PersonWithId) p1;
Console.WriteLine("Conversion succeeded.");
}
catch (InvalidCastException) {
Console.WriteLine("Conversion failed.");
}
Person p2 = null;
try {
PersonWithId pid2 = (PersonWithId) p2;
Console.WriteLine("Conversion succeeded.");
}
catch (InvalidCastException) {
Console.WriteLine("Conversion failed.");
}
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:61,代码来源:InvalidCastException 输出:
Conversion failed.
Conversion succeeded.
Conversion succeeded.
示例3: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
var culture = CultureInfo.InvariantCulture;
IFormatProvider provider = culture;
DateTimeFormatInfo dt = (DateTimeFormatInfo) provider;
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:14,代码来源:InvalidCastException 输出:
Unhandled Exception: System.InvalidCastException:
Unable to cast object of type //System.Globalization.CultureInfo// to
type //System.Globalization.DateTimeFormatInfo//.
at Example.Main()
示例4: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
object value = 12;
// Cast throws an InvalidCastException exception.
string s = (string) value;
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:12,代码来源:InvalidCastException
示例5: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
object value = 12;
string s = value.ToString();
Console.WriteLine(s);
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:12,代码来源:InvalidCastException 输出:
12
注:本文中的System.InvalidCastException类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论