[IronPython] SystemError when inspecting an Assembly File through Assembly.ReflectionOnlyLoadFrom()

John Messerly jomes at microsoft.com
Thu Jun 28 23:35:25 CEST 2007


I think what we're seeing here is an IronPython bug. When you try to call a method on "a", we reflect over the assembly looking for custom attributes, which fails because it was loaded with ReflectionOnlyLoadFrom. If it's any consolation, it is fixed in the 2.0 alpha tree:

IronPython console: IronPython 2.0 (2.0.10625.05) on .NET 2.0.50727.1318
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import sys
>>> from System.Reflection import *
>>> a = Assembly.ReflectionOnlyLoadFrom('Test.exe')
>>> an = a.GetName()
>>> print an.Name + " " + str(an.Version)
Test 1.0.0.0

In 1.1, it looks like you can get the assembly version string by doing "str(a)". That doesn't get you the list of referenced assemblies though. The easiest workaround is probably to create a C# helper DLL to do the reflection piece, and then call that from IronPython.

- John


-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of C L
Sent: Thursday, June 28, 2007 1:41 PM
To: users at lists.ironpython.com
Subject: [IronPython] SystemError when inspecting an Assembly File through Assembly.ReflectionOnlyLoadFrom()

Hello,

I am just playing around with IronPython (1.0 and 1.1 on .NET 2.0.50727.42)
and I tried to access the version and the assembly name of an assembly (DLL or EXE)
through reflections. (I would like to get the referenced assemblies as well, but that is a second step)

While the following works in C# code work (with eg it's own EXE as argument)
------
using System;
using System.Reflection;

class Program
{
    static void Main(string[] args)
    {
        Assembly a = Assembly.ReflectionOnlyLoadFrom(args[0]);
        AssemblyName an = a.GetName();
        Console.Write("{0} {1}", an.Name, an.Version);
    }
}
------


With the (IMHO) equivalent IronPython code
------
import sys
from System.Reflection import *
a = Assembly.ReflectionOnlyLoadFrom(sys.argv[1])
an = a.GetName()
print an.Name + " " + an.Version
------

I get following error (VersionInfo.exe is the compiled version of the above C# code)
> ipy.exe AssemblyVersion.py bin\Debug\VersionInfo.exe
Traceback (most recent call last):
  File AssemblyVersion.py, line 4, in Initialize
SystemError: It is illegal to reflect on the custom attributes of a Type loaded via ReflectionOnlyGetType (see Assembly.ReflectionOnly) -- use CustomAttributeData instead.

I tried to use CustomAttributeData  as suggested by the error but didn't succeed
(I simply don't understand how to use it).
It seemed terrible complicated to access the version.

It would be possible to parse str(a) but I can't believe this is
the way to go.

Are there any other ways to access this information or
what am I missing? Or any hints?

Regards,
Claudius


Btw: I tried to send this may before, but didn't get any reply nor
could I find it in the archive. So please excuse if you get it a second time.



More information about the Ironpython-users mailing list