[IronPython] ArgumentTypeException when accessing C# objects, need help!

Dino Viehland dinov at microsoft.com
Tue Sep 23 22:35:30 CEST 2008


Yeah, unfortunately with the bad error message it's really one of those things you need to have run into at some point. :(

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Derek Ealy
Sent: Tuesday, September 23, 2008 1:30 PM
To: Discussion of IronPython
Subject: Re: [IronPython] ArgumentTypeException when accessing C# objects, need help!


Man, that worked! I've been really bummed about not figuring this out for the past day or so. Thanks a lot for the tip!!!!!!

Derek


-------------------------Derek Blogging at:

----------------------------------------
> From: dinov at microsoft.com
> To: users at lists.ironpython.com
> Date: Tue, 23 Sep 2008 12:26:55 -0700
> Subject: Re: [IronPython] ArgumentTypeException when accessing C# objects,    need help!
>
> This is probably due to a loader context issue.  Effectively what's happening is your clr.AddReference() from your Python script is probably ultimately doing a LoadFile (putting the assembly into a special context) and otherwise your assembly is getting loaded normally - so you end up w/ two copies of the same assembly loaded but w/ different contexts.
>
> This is much easier to do from 2.0 where you can just call ScriptRuntime.LoadAssembly and pass it in your assembly object (e.g. typeof(Entry).Assembly).  It looks like you're using 1.0 though...  What might be easiest these is to right a small piece of Python code which does:
>
> import clr
> clr.AddReference(myAssembly)
>
> where myAssembly is a global that points to typeof(Entry).Assembly.
>
> Alternately if your assembly has a strong name (maybe it needs to be in the GAC as well?) you could do clr.AddReference() with the fully qualified strong name.
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Derek Ealy
> Sent: Tuesday, September 23, 2008 12:07 PM
> To: users at lists.ironpython.com
> Subject: [IronPython] ArgumentTypeException when accessing C# objects, need help!
>
>
> Hi
>
> I'm pretty new to this whole IronPython thing and have a problem that's been hanging me up for a couple of days now. I have a fairly large C# application, and we've recently added IronPython to the mix so we can support dynamic scripting at runtime.
>
> Users are able to create Python scripts which can manipulate the C# objects in our main application. The problem that I'm having is when I try to pass a C# object to a method on another C# object from within the Python script. It is throwing an ArgumentTypeException: expected Entry got Entry. This is especially confusing, because it sure looks like the exception is saying that an Entry object was expected, and that indeed an Entry object is what it got.
>
> Here is an example of what I'm trying to do:
>
> // C# code
>
> PythonEngine py = new PythonEngine();
>
> string init = "import clr\n" +
>          "clr.AddReference(\"TritonConfiguration.dll\")\n" +
>          "import TritonConfiguration.ValidationReport as ValidationReport\n" +
>          "import TritonConfiguration.Entry as Entry";
>
> py.Execute(init);
>
> Entry entry = new Entry();
> entry.PrimaryKey.SetValue("Fred");
> Rule rule = new Rule();
> rule.Name = "testRule";
> py.Globals["entry"] = entry;
> py.Globals["rule"] = rule;
>
> string pyLogic = "if entry.PrimaryKey.Value != 'Barney':\n" +
>         "   failure = ValidationReport.RuleFailure('PK is not Barney')\n" +
>         "   failure.ruleName = rule.Name\n" +
>         "   failure.failedEntry = entry\n";
>
> py.Execute(pyLogic);
>
>
>
> It is the last line in the pyLogic string that causes the ArgumentTypeException. The failedEntry property of the RuleFailure object is of type Entry. I'm not sure if there is a problem with the way I'm importing things from TritonConfiguration.dll, or the assigning and usage of global variables in Python, or if there's some other thing wrong that I'm completely unaware of.
>
> Any suggestions would be greatly appreciated.
>
>
> -------------------------
> Derek
>
> Blogging at: http://derekealy.com/WordPress
>
> _________________________________________________________________
> See how Windows Mobile brings your life together-at home, work, or on the go.
> http://clk.atdmt.com/MRT/go/msnnkwxp1020093182mrt/direct/01/
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

_________________________________________________________________
See how Windows Mobile brings your life together-at home, work, or on the go.
http://clk.atdmt.com/MRT/go/msnnkwxp1020093182mrt/direct/01/
_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list