clr.AddReferenceByPartialName(
PartialAssemblyName
)
: Adds a reference to the .NET
assembly by passing a partial name that IronPython looks up in the GAC. This method doesn’t
assure that you obtain any particular version of the assembly you need. You can use the
clr.AddReferenceByName()
method to better control which version of the assembly loads.
Now that you have a better idea of how to add a reference, let’s try importing the System.Xml
assembly. The following steps help you get the assembly referenced and imported into IronPython.
1.
Type
import sys
and press Enter. This step makes the
sys
module accessible.
2.
Type
sys.path.append(‘C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727‘)
and press Enter. In order to add a reference to an assembly, you must provide its location as
part of
sys.path
. You may need to change the drive and directory to match your system.
If you provide the wrong .NET Framework location, you can use the
sys
.path.remove()
method to remove it from the list. For example, if you want
to remove the .NET Framework 2.0 path, you type
sys.path.remove(‘C:\\
WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727‘)
and press Enter.
3.
Type
import clr
and press Enter. This step makes the
clr
module accessible, which has the
various assembly reference methods described earlier.
4.
Type
clr.AddReference(‘System.Xml.DLL‘)
and press Enter. IronPython now has a ref-
erence to the assembly file it needs, but the assembly isn’t imported yet. If you receive an IO
Error message, it means that IronPython couldn’t find the assembly you requested in the loca-
tion provided as part of
sys.path
.
5.
Type
import System.Xml
and press Enter. The
System.Xml
assembly is now available for
use. It’s time to test to verify that the assembly is available.
6.
Type
dir(System.Xml)
and press Enter. You should see the content of the
System.Xml
assembly, as shown in Figure 7-9.
This technique works with any .NET assembly, not just those found in the GAC. If you have a custom
.NET assembly you want to use in your application, this technique lets you access it with ease. Make
sure you use the right technique for the kind of assembly you want to import. If version number is
important, then make sure you use the
clr.AddReferenceByName()
method.
548592c07.indd 125
2/25/10 9:44:27 AM
www.finebook.ir
Do'stlaringiz bilan baham: |