[ Pobierz całość w formacie PDF ]
.")End SubOverrides Public Sub TurnRight( )Console.WriteLine("Car turns right.")End SubOverrides Public Sub ApplyBrakes( )Console.WriteLine("Car trying to stop.")throw new Exception("Brake failure!")End SubEnd Class56 .NET Framework EssentialsWith this code, we can build a DLL using the command-line VB.NET compiler, as follows:vbc /r:vehicle.dll /t:library /out:car.dll car.vbSince we want the VB.NET compiler to generate a DLL, we must signal this by using the /t:libraryoption.Also, since Car derives from Vehicle, the VB.NET compiler must resolve the references toVehicle.We can tell the VB.NET compiler the location of external references using the /r: option.It isimportant to note that you don't need to have the source code for the vehicle DLL to reuse its codebecause all type information can be obtained from any.NET assembly.In addition, you should notethat from this example, we have proven that you can derive a VB.NET class from a Managed C++class.3.3.3 Plane Class in C#Now let's use C# to develop the Plane class, which derives from the Vehicle class written in ManagedC++.Similar to the Car class, the Plane class implements the three virtual functions from the Vehicleclass.Unlike the Car class, though, the ApplyBrakes( ) method of this class doesn't throw anexception.using System;public class Plane : Vehicle{override public void TurnLeft( ){Console.WriteLine("Plane turns left.");}override public void TurnRight( ){Console.WriteLine("Plane turns right.");}override public void ApplyBrakes( ){Console.WriteLine("Air brakes being used.");}}You can build a DLL from this code using the following command:csc /r:vehicle.dll /t:library /out:plane.dll plane.csNotice that we have used the /r: option to tell the C# compiler that Vehicle is defined in vehicle.dll.3.3.4 Test Driver in C#Having developed vehicle.dll, car.dll , and plane.dll, we are now ready to demonstrate thatpolymorphism and exception handling work across different languages.Written in C#, the upcomingcode listing contains a Main( ) method with a Vehicle reference and an exception handler.Inside the try block, we first instantiate a Plane class and refer to this instance using the local Vehiclereference.Instead of telling the Plane to TurnLeft( ) or ApplyBrakes( ), we tell the Vehicle to do so.Similarly, we instantiate a Car and refer to this instance using the local Vehicle reference.Again,instead of telling the Car to TurnLeft( ) or ApplyBrakes( ), we tell the Vehicle to do s o.In both cases,we tell the Vehicle either to TurnLeft( ) or ApplyBrakes( ), but the actual vehicle that employs57 TurnLeft( ) or ApplyBrakes( ) is the Plane instance in the first case and the Car instance in the secondcase; that's polymorphism, and it works across languages.You should note that the second call to ApplyBrakes( ) would cause an exception because we threwan exception from Car's ApplyBrakes( ).Although Car's ApplyBrakes( ) was written using VB.NET, wecould still catch the exception that it's throwing in C#, proving that exception handling works acrosslanguages.using System;class TestDrive{public static void Main( ){Vehicle v; // Vehicle referencetry{Plane p = new Plane( );v = p;v.TurnLeft( );v.ApplyBrakes( );Car c = new Car( );v = c;v.TurnLeft( );v.ApplyBrakes( ); // Exception}catch(Exception e){Console.WriteLine(e.ToString( ));}}}If you want to test out these features, you can create an EXE using the following command:csc /r:vehicle.dll;car.dll;plane.dll /t:exe /out:drive.exe drive.csSince we have used the Vehicle, Car, and Plane classes in this code, we must include references tovehicle.dll, car.dll, and plane.dll.In addition, since we are building an EXE, we need to signify this tothe C# compiler using the /t:exe option.Once you have built this EXE and executed it, you get thefollowing output:Plane turns left.Air brakes being used.Car turns left.Car trying to stop.System.Exception: Brake failure!at Car.ApplyBrakes( )at TestDrive.Main( )As expected, the plane first turns left and then uses its air brakes.Then the car turns left, tries to stop,but can't, so it throws an exception, which is caught in the Main( ) method.In this simple example, we have shown that you can now take advantage of inheritance, polymorphism,and exception handling across different languages in the.NET Framework.58 .NET Framework Essentials3.4 SummaryWe started this chapter by tellin g you that.NET provides a common programming model, whichreduces the learning curve and increases productivity.Once you've learned how to do somethingusing the classes in the.NET Framework, this knowledge will transfer to any.NET language.We thenillustrated that we could write the same type of code, supporting major.NET features, in any givenlanguage.Finally, we proved to you that.NET indeed supports language integration, which issomething never before possible using Microsoft platforms and tools, prior to.NET.59 60 .NET Framework EssentialsChapter 4.Working with.NET ComponentsHaving seen the language-integration examples in the previous chapter, we know that all.NETassemblies are essentially binary components.[1] You can treat each.NET assembly as a componentthat you can plug into another component or application, without the need for source code, since allthe metadata for the component is stored inside the.NET assembly.While you have to perform a tonof plumbing to build a component in COM, you need to perform zero extra work to get a componentin.NET, as all.NET assemblies are components by nature.[1]Remember, as we explained in Chapter 1 , we're using the term "component" as a binary, deployable unit, not as a COM class.In this chapter, we examine the more advanced topics, including component deployment, distributedcomponents, and enterprise services, such as transaction management, object pooling, role-basedsecurity, and message queuing.4.1 Deployment OptionsFor a simple program like hello.exe that we built in Chapter 2 , deployment is easy: copy theassembly into a directory, and it's ready to run.When you want to uninstall it, remove the file from thedirectory [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • wpserwis.htw.pl