Hello, World (Component Version)

In terms of object-oriented programming, C# and the other .NET languages are nice, but the real innovation of .NET comes from the ease with which components can be created. A component can be thought of as a piece of black-box functionality, and different components can be hooked together as long as the proper external interfaces are observed. Components of a stereo system are a good example. You can plug in different speakers, and they will still work, provided the speakers use the standard interface.

In software there have been a number of different component architectures. Before .NET the leading component architecture on Microsoft systems was the Component Object Model (COM), which did indeed provide black-box reusability of software modules written in different languages. But COM requires either the program (in languages like C++) or the programming environment (in languages like Visual Basic) to implement the plumbing code required for interoperability. COM is complex and places a substantial burden on the programmer.

The beauty of .NET is that the .NET Framework itself transparently provides all the needed plumbing, and creating a .NET component out of a class is as easy as compiling as a special kind of project, a class library. Once built, other programs can call this class library merely by obtaining a reference to it. These other programs can be in any .NET language, including PerlNET.

Hello, World Class Library

As an example of creating a class library in C#, we will create a component version of our Hello class. A completed solution is in HelloLib. The source files are identical. We just compile them in a different way. To build the class library Hello.dll at the command line, use the /t:library switch (short for /target:library).


csc /t:library Hello.cs

To use the class library, you need to obtain a reference to it, which you can do when you build the EXE program by using the /r switch (short for /reference).

csc /out:TestHello.exe /r:Hello.dll TestHello.cs

These two compile commands are provided in the batch file build.bat.

A PERL CLIENT PROGRAM

You can easily create a client program TestHello.pl that uses this class library. See the folder HelloPerl. For convenience we have the C# class source file Hello.cs and the Perl file TestHello.pl together in the same folder, along with a batch file build.bat that builds both. (We also provide a Visual Studio solution.) Here is the Perl script.


#
# HelloPerl.pl
#

use strict;
use namespace "System";
use PerlNET qw(AUTOCALL);

my $obj = Hello->new();
$obj->SayHello();
$obj = Hello->new("Goodbye, world");
$obj->SayHello();
$obj->{Greeting} = "Brave New World";
Console->WriteLine($obj->{Greeting});

You may wish to review the Chapter 10 discussion of using .NET components in PerlNET programs.

In the rest of this appendix we will stick strictly to C# programs. As an exercise, you may wish to rebuild the C# classes as class libraries and create PerlNET programs to exercise them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.33.41