您的位置:首页 > 其它

丶使用is关键字判断对象是否与指定类型兼容

2011-10-17 01:32 459 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Example24
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
object P_obj = radioButton1.Checked ? // 正确地为变量添加引用
(object)"C# 编程词典" : new System.IO.FileInfo(@"d:\"); //@除去转义字符效果
if (radioButton4.Checked) //判断选择了哪一个类型
{
if (P_obj is System.String) //判断是否字符串类型
{
MessageBox.Show("对象与指定类型兼容", "提示"); //提示兼容信息
}
else
{
MessageBox.Show("对象与指定类型不兼容", "提示"); //提示不兼容信息
}
}
else
{
if (P_obj is System.IO.FileInfo)
{
MessageBox.Show("对象与指定类型兼容", "提示"); //提示兼容信息
}
else
{
MessageBox.Show("对象与指定类型不兼容", "提示"); //提示不兼容信息
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: