丶使用as关键字将对象转换为指定类型
2011-10-17 01:33
239 查看
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;
using System.IO;
namespace Example26
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
using (FileStream P_filestream = //创建文件流
new FileStream(@"D:\My Documents\Visual Studio 2008\Projects\Example26\Log.txt", FileMode.Create))
{
object P_object = P_filestream as object; //使用as关键字转换类型
if (P_object != null) //判断转换是否成功
{
MessageBox.Show("转换为 object 成功!", "提示");
}
else
{
MessageBox.Show("转换 object 失败!", "提示");
}
}
}
if (radioButton2.Checked)
{
using (FileStream P_filestream = //创建文件流
new FileStream(@"D:\My Documents\Visual Studio 2008\Projects\Example26\Log.txt", FileMode.Create))
{
object P_obj = P_filestream;
Stream P_stream = P_obj as Stream; // 使用as关键字转换类型
if (P_stream != null) //判断是否转换成功
{
MessageBox.Show("转换为 Stream 成功!", "提示");
}
else
{
MessageBox.Show("转换 Stream 失败!", "提示");
}
}
}
if (radioButton3.Checked)
{
using (FileStream P_filestream = //创建文件流
new FileStream(@"D:\My Documents\Visual Studio 2008\Projects\Example26\Log.txt", FileMode.Create))
{
object P_obj = P_filestream;
string P_str = P_obj as string; //使用as关键字转换类型
if (P_str != null) //判断是否转换成功
{
MessageBox.Show("转换为 string 成功!", "提示");
}
else
{
MessageBox.Show("转换 string 失败!", "提示");
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Example26
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
using (FileStream P_filestream = //创建文件流
new FileStream(@"D:\My Documents\Visual Studio 2008\Projects\Example26\Log.txt", FileMode.Create))
{
object P_object = P_filestream as object; //使用as关键字转换类型
if (P_object != null) //判断转换是否成功
{
MessageBox.Show("转换为 object 成功!", "提示");
}
else
{
MessageBox.Show("转换 object 失败!", "提示");
}
}
}
if (radioButton2.Checked)
{
using (FileStream P_filestream = //创建文件流
new FileStream(@"D:\My Documents\Visual Studio 2008\Projects\Example26\Log.txt", FileMode.Create))
{
object P_obj = P_filestream;
Stream P_stream = P_obj as Stream; // 使用as关键字转换类型
if (P_stream != null) //判断是否转换成功
{
MessageBox.Show("转换为 Stream 成功!", "提示");
}
else
{
MessageBox.Show("转换 Stream 失败!", "提示");
}
}
}
if (radioButton3.Checked)
{
using (FileStream P_filestream = //创建文件流
new FileStream(@"D:\My Documents\Visual Studio 2008\Projects\Example26\Log.txt", FileMode.Create))
{
object P_obj = P_filestream;
string P_str = P_obj as string; //使用as关键字转换类型
if (P_str != null) //判断是否转换成功
{
MessageBox.Show("转换为 string 成功!", "提示");
}
else
{
MessageBox.Show("转换 string 失败!", "提示");
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
相关文章推荐
- 使用类型转换器将定文本转换为指定对象 (转)
- 丶使用is关键字判断对象是否与指定类型兼容
- 在c++中,有哪4个与类型转换相关的关键字,这些关键字各有什么特点,应该在什么场合下使用?
- Swift开发之 as、as!、as?三种类型转换操作符使用详解
- Swift as、as!、as?三种类型转换操作符使用详解
- Swift开发之 as、as!、as?三种类型转换操作符使用详解
- c++类型转换关键字dynamic_cast和static_cast的使用区别
- 使用stringstream对象简化类型转换
- (转)使用stringstream对象简化类型转换
- 使用stringstream对象实现类型转换实例
- c++ 使用stringstream对象简化类型转换
- 【面试题001】类型转换关键字,空类对象模型,拷贝构造函数,赋值运算符函数
- 【C++】使用stringstream对象简化类型转换
- Effective C#阅读笔记-3使用is或者as代替cast做类型转换
- 写几个带继承关系的类,使用直接强转的方式,以及使用as转换, 并且练习is的类型判断
- Swift开发之 as、as!、as?三种类型转换操作符使用详解
- 使用stringstream对象简化类型转换
- 【面试题001】类型转换关键字,空类对象模型,拷贝构造函数,赋值运算符函数
- Swift - as、as!、as?三种类型转换操作使用一览
- 《Effective C#》Item 3:使用as和is操作符来做类型转换