早教吧作业答案频道 -->英语-->
英语翻译Programmersfinditfrequentlynecessarytocastanobjectfromonetypetoanother.Yourprogramminglanguageofchoicedecideshoweasytoexposecastingoperationstothedeveloper.Forexample,C#doesn'trequireanyspecialsyntaxtoca
题目详情
英语翻译
Programmers find it frequently necessary to cast an object from one type to another.Your programming language of choice decides how easy to expose casting operations to the developer.For example,C# doesn't require any special syntax to cast a type to any of its base types.However,C# does require that the developer explicitly cast a type to any of its derived types.The code below demonstrates:
The text above discusses what is necessary for your compiler of choice to compile your code.We will now discuss what happens at runtime.
At runtime,the CLR always checks casting operations to ensure that casts are always to the object's actual type or any of its base types.For example,the code below will throw an InvalidCastException at runtime:
In the Main method above,a MyType object is constructed and this object is passed to SomeMethod.For example,calling SomeMethod is OK,since MyType is derived from Object which is what SomeMethod expects.Once inside SomeMethod,the CLR verifies that o refers to an object that is either a MyType or a type that is derived from MyType.Since MyType is a MyType,the CLR performs the cast and allows SomeMethod to continue executing.
After SomeMethod returns,the Main function constructs a DateTime object and passes it to SomeMethod.Again,a DateTime is derived from Object so the compiler compiles the code that calls SomeMethod.However,inside SomeMethod,the CLR verifies the cast and detects that o refers to a DateTime object that is not a MyType or any type derived from MyType.At this point,the CLR cannot allow the cast and instead,the CLR throws an InvalidCastException.
If the CLR allowed the cast,then there is no type safety and types could easily spoof other types.Type spoofing is the cause of many security breaches and compromises an application's stability and robustness.Type safety is an extremely important part of the .NET Framework
Programmers find it frequently necessary to cast an object from one type to another.Your programming language of choice decides how easy to expose casting operations to the developer.For example,C# doesn't require any special syntax to cast a type to any of its base types.However,C# does require that the developer explicitly cast a type to any of its derived types.The code below demonstrates:
The text above discusses what is necessary for your compiler of choice to compile your code.We will now discuss what happens at runtime.
At runtime,the CLR always checks casting operations to ensure that casts are always to the object's actual type or any of its base types.For example,the code below will throw an InvalidCastException at runtime:
In the Main method above,a MyType object is constructed and this object is passed to SomeMethod.For example,calling SomeMethod is OK,since MyType is derived from Object which is what SomeMethod expects.Once inside SomeMethod,the CLR verifies that o refers to an object that is either a MyType or a type that is derived from MyType.Since MyType is a MyType,the CLR performs the cast and allows SomeMethod to continue executing.
After SomeMethod returns,the Main function constructs a DateTime object and passes it to SomeMethod.Again,a DateTime is derived from Object so the compiler compiles the code that calls SomeMethod.However,inside SomeMethod,the CLR verifies the cast and detects that o refers to a DateTime object that is not a MyType or any type derived from MyType.At this point,the CLR cannot allow the cast and instead,the CLR throws an InvalidCastException.
If the CLR allowed the cast,then there is no type safety and types could easily spoof other types.Type spoofing is the cause of many security breaches and compromises an application's stability and robustness.Type safety is an extremely important part of the .NET Framework
▼优质解答
答案和解析
好难啊!
看了 英语翻译Programmer...的网友还看了以下:
云一剐,玉一梭,澹澹衫儿薄薄罗.这里的澹、薄应该怎么读呢?薄拼音:[báo][bó][bò]澹拼音 2020-05-22 …
A.殆(yí)嗟乎(jié)针砭时弊(biǎn)B.郯(tán)严谨(jìn)不拘于时(jū)C. 2020-06-22 …
下列加线字的注音,不全部正确的一组是()A.孩稚(zhì)地毯(tǎn)淘(táo)气虔(qián 2020-06-29 …
小小变色龙.禁j小小变色龙.禁jīn()通tōng()澄chéng()jiě()jìn()tòng 2020-07-02 …
1.已知数列{a(n)}满足a(n)a(n+1)a(n+2)a(n+3)=24,且a1=1a2=2 2020-07-09 …
什么是二项式的通式?在二项式定理(a+b)^n=C(n,0)a^n+C(n,1)a^(n-1)b+ 2020-07-31 …
数列an满足递推式(a(n+2))*an-(a(n+1))^2=(t^n)*(t-1),a1=1, 2020-08-01 …
已知一个边长为a的等边三角形,现将其边长n(n为大于2的整数)等分,并以相邻等分点为顶点向外作小等 2020-08-01 …
A.郯子(tán)B.句读(dòu)C.阿谀(yú)D.或不焉(fǒu)六艺经传(chuán)苌弘( 2020-11-26 …
A.煊(xuān)羊肉蛰居(zhé)间或(jiān)余晖(huī)B.丰腴(yú)赭(zhě)色霖( 2020-12-11 …