IronRuby > Documentation > .NET Integration > Referencing Assemblies

Referencing Assemblies

From $1

Ruby uses two methods for running other Ruby files: Kernel#require and Kernel#load. load will execute the file provided every time it’s called, while require only executes the file once. Ruby can also load linked libraries (.so files) on UNIX systems, so IronRuby takes advantage of this behavior to load DLLs.

By File Name

Given an arbitrary CLR assembly placed on the IronRuby $LOAD_PATH, say models.dll, it can be required by its file name.

>>> require 'models.dll'
=> true

The assembly is loaded via Assembly.LoadFile API.

You can also omit the ".dll" or ".exe" extension if there are no .rb files of the same name on the $LOAD_PATH.

>>> require 'models'
=> true

           

By Strong Name

Assemblies can either be signed or unsigned. The above example works for both, however, it is recommended to load signed assemblies using their strong name. A strong name is made up of the name, version, culture, and public key. For example, IronRuby.dll’s strong name is IronRuby, Version=0.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35. Unsigned assemblies also have strong names, but they have PublicKeyToken=null instead.

To load a signed assembly, give require or load the strong name:

>>> require "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
=> true

The assembly is loaded via Assembly.Load API. 
To find the strong name for an assembly, use the Strong Name Utility (sn.exe) or Red Gate’s .NET Reflector.

IronRuby has a libs directory on the load path which contains Ruby files for common .NET libraries like WinForms, WPF, System.Data.dll, etc, which just requires that library by its strong name. Now requiring WinForms can just be:

>>> require "System.Windows.Forms"
=> true

Be explicit

To be more explicit about loading assemblies over other Ruby files, use the load_assembly method. This method is defined on Kernel module and is an IronRuby extension. The first argument is the name of the assembly to load. IronRuby tries to load the assembly via Assembly.Load. If the assembly is not found Assembly.LoadWithPartialName is called.

Using a strong name:

>>> load_assembly "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
=> true

Using a partial name:

>>> load_assembly "System.Windows.Forms"
=> true

Although convenient for interactive exploration using a partial name is not recommended for production code. See Assembly.LoadWithPartialName remarks.

Optionally, you can pass a second argument, which is a full name of a namespace declared in an assembly that implements a Ruby library. This is an advanced scenario primarily intendend for standard libraries. For example, the following code loads stringio library implemented in IronRuby.Libraries.dll:

>>> load_assembly 'IronRuby.Libraries', 'IronRuby.StandardLibrary.StringIO'
=> true

All standard libraries have their loader scripts in libs directory so that they can be loaded in the way Ruby programmers are used to. For example, the script libs\stringio.rb contains the call to load_assembly shown above.

>>> require 'stringio'
=> true
Tags:
 
Images (0)
 
Comments (0)
You must login to post a comment.

 
SourceForge.net