首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

CustomerEntity innerModel = model as CustomerEntity;//是什么意思?解决办法

2011-12-30 
CustomerEntity innerModel model as CustomerEntity//是什么意思?CustomerEntityinnerModelmodelasCu

CustomerEntity innerModel = model as CustomerEntity;//是什么意思?
CustomerEntity   innerModel   =   model   as   CustomerEntity;是什么意思?


CustomerEntity   这个是类名   innerModel   是new对象     吗?   model   as   CustomerEntity


[解决办法]
1。
as,是C#中提供的显示转换操作符

具体参考:
http://msdn2.microsoft.com/zh-cn/library/cscsdfbt(VS.80).aspx


2。
CustomerEntity innerModel = model as CustomerEntity;
==========
表示 对象变量 model 强制转换为 CustomerEntity

功能上等同于

CustomerEntity innerModel = (CustomerEntity)model;

区别在于,当 model 是一个无法转换为 CustomerEntity 的对象时候,
前者,直接返回 null (空引用)
后者,直接丢出转换无效异常 System.InvalidCastException


[解决办法]
CustomerEntity innerModel = model as CustomerEntity;

把一个model对象转换为CustomerEntity类的对象,如果成功,将其引用地址赋给CustomerEntity类的引用innerModel,如果失败,innerModel返回null

热点排行