安全轉換參考型別(转)

发布时间: 2007-05-21 09:43    作者: 未知    来源: 未知    浏览:    评论

對初學者來說經常造成初學者問題的是參考型別(reference types )的轉換(conversion),關於這點在C++ 造成相當的困擾更是如此,幸運的是,在 C# 中不會發生。盡管是或是因為此種型別安全語言,對於不同的技術都應該要有所認識。

轉型
is 運算子
as 運算子
為了能夠更清楚說明這個主題,我選擇一個很不錯的例子:我們試著將 apples 轉換成 oranges - 某些地方是 C# 不允許的(因此較適合用來說明如何處理轉換錯誤) 所有範例的基本程式碼架構如下 (classes.aspx):

<script language=c# runat=server>
class apple
{
  public string Fruit() { return "Apple"; }
}

class orange
{
  public string Fruit() { return "Orange"; }
}
</script>
<% @page language=c# %>
<%
apple myApple = new apple();
orange myOrange = new orange();
Response.Write(myApple.Fruit()+"<br>");
Response.Write(myOrange.Fruit()+"<br>");
%>

轉型 (Casting)
即使我們會失敗,我們所先會試著去解決:

apple myApple = new apple();
orange anotherOrange = (orange)myApple;

上面程式碼編譯器會抱怨:它知道這個轉換是無效的,甚至不會去編譯程式碼。 理由很清楚,就是 apple class 與 orange class 根本沒有相同之處。

所以我們將選擇另一個較實際的方法 - 首先將型別存放在一般 object 變數中(例如當將元素存放在堆疊 (Stack),Queues 或 ArrayLists 中), 之後在轉換成可再使用的適當型別:

apple myApple = new apple();
// the following line stands for potentially more complicated operations
object objGeneral = myApple;
orange anotherOrange = (orange)objGeneral;

當選擇了錯誤型別 - 就如我們所做的,C# 可能就不允許這樣 - 也就是說在執行時期可藉由InvalidCastException,要能適當捕獲錯誤,例外處理程式碼是必須的,例如:

apple myApple = new apple();
object objGeneral = myApple;
try
{
   orange anotherOrange = (orange)objGeneral;
   // do whatever you want with the orange
}
catch(InvalidCastException e)
{
   // either do nothing or tell the user
}

即使是所有的錯誤都處理了,在這例外處理中我只發現到一失敗地方。如果我能夠事先的 要求轉型是否成功,可能情況會更好更方便。這也就是為何 is 運算子會派上用場之處。

is 運算子
因為已經宣告了,is 運算子允許要求轉型是否成功或是產生例外。 下面這段小程式說明 is 運算子如何在應用程式中使用:

apple myApple = new apple();
object objGeneral = myApple;

if (objGeneral is orange)
{
   orange anotherOrange = (orange)objGeneral;
   // do whatever you want with the orange
}
else
{
   Response.Write("this is no orange");
}

我想給一個關於is 運算子的提示 - 如果在編譯執行時候可以決定轉換 不是成功就是失敗的話 - 關於這一點我們很遺憾的是在 ASP.NET 看不到。

as 運算子
as 運算子可以稱作是 exception-less cast。轉換會暗地執行 - 萬一轉換失敗,要轉換的目標變數就會設成null。所有剩下的工作就 是使用 if-statement 陳述來檢查這個變數:

apple myApple = new apple();
object objGeneral = myApple;

orange anotherOrange;
anotherOrange = objGeneral as orange;
if (null != anotherOrange)
{
   // do whatever you want with the orange
}
else
{
   Response.Write("this is no orange");
}

對 as 運算子來說道理就如同 is 運算子一樣 - 如果轉換成功或是失敗,你會收到警告訊息。 例如這個程式碼:

apple myApple = new apple;
apple anotherApple = myApple as apple;

結論
條條道路通羅馬,到羅馬的道路可以有好幾條路,然而有些道路會比其他道路曲折。 對於型別轉換我個人最喜歡的方法是用 is運算子。

TAG

Smile Big Smile Surprise Stick out tongue Wink Sad Tongue Tied Indifferent Crying Embarrassed Cool Angry Angel Devil [8-|] [:#] [:-*] [:^)] [<:o)] [|-)] Yes Beer Left Hug Music Star Time Snail Pizza Automobile Umbrella Computer Storm [mo] [8o|] [^o)] [+o(] [*-)] [8-)] Coffee No Drinks [Z] Right Hug Cake Broken Heart Gift Wilted Flower Movie Dog Idea Sleep Email Travel Paradise
呢称:

加粗 斜体 下划线 链接 图片 代码 邮件地址 引用 列表

最多只能输入100个字符

Tags

SQL 数据库 asp.net C# XML 控件 .NET教程 程序 事件 数据 安全 代码 Server 客户端 验证 数据库专栏 接口 文件 Oracle DataSet 函数 DataGrid 问题 .net return C#语言 JavaScript 服务 IIS 对象 语句 windows 继承 时间 web.config 设计 开发 参数 变量 解决 字符 ADO.net 环境 VB.Net语言 web 异常 工具 服务器 计算 实例 OLEDB Application VB Word WebService insert asp net 安装 记录

精华推荐

更多

精品下载

更多