代码如下:
private void btnConvertType_Click(object sender, EventArgs e) { if (rdo_btn_ConvertObject.Checked)//如果选择转换为object类型 { using(FileStream filestram = new FileStream(@"d:\log.txt",System.IO.FileMode.Create))//创建文件文件流对象 { object _object = filestram as object;//使用as关键字转换类型 if(_object != null)//判断转化是否成功 MessageBox.Show("转换为Object成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); else MessageBox.Show("转换为Object未成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } if (rdo_btn_ConvertStream.Checked)//如果选择转换为Stream类型 { using(FileStream filestream = new FileStream(@"d:\log.txt",System.IO.FileMode.Create))//创建文件流对象 { object _object = filestream; Stream _stream = _object as Stream;//使用as关键字转换类型 if (_stream != null) MessageBox.Show("转换为Stream成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); else MessageBox.Show("转换为Stream未成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } if (rdo_btn_ConvertString.Checked)//如果选择转换为string类型 { using (FileStream filestream = new FileStream(@"d:\log.txt",System.IO.FileMode.Create))//创建文件流 { object _object = filestream; string _string = _object as string; if (_string != null) MessageBox.Show("转换为String成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); else MessageBox.Show("转换为String未成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }