English 中文(简体)
Why can t I import my C# type into IronPython?
原标题:

I have some types in a C# library I wrote, e.g.:

namespace SprocGenerator.Generators
{
    public class DeleteGenerator : GeneratorBase
    {
        public DeleteGenerator(string databaseName, string tableName) : base(databaseName, tableName)

I want to use them in an IronPython script:

import clr
import sys

clr.AddReferenceToFile("SprocGenerator.dll")
# problem happens here:
from SprocGenerator.Generators import *

generator = DeleteGenerator("a", "b")

When the line below the comment happens, I get:

ImportError: No module named Generators

I have verified that the file I am loading is what I expect by renaming it and verifying the script throws an error when trying to load the assembly. I have verified the namespace is in the assembly via Reflector. I have also tried specifying a fully-qualified classname to work around my import issue, e.g.

 generator = SprocGenerator.Generators.DeleteGenerator("a", "b")

But I get:

 NameError: name  SprocGenerator  is not defined

Even if I have this in C#:

namespace SprocGenerator
{
    public static class GeneratorHelper
    {
        public static string GetTableAlias(string tableName)

And this in IP:

import clr
import sys
from System import *

clr.AddReferenceToFile("SprocGenerator.dll")
from SprocGenerator import *

print "helper = " + GeneratorHelper.GetTableAlias("companyBranch")

I get this error:

 NameError: global name  GeneratorHelper  is not defined

What am I doing wrong?

问题回答

Could you be picking up the DLL from a different location then you re expecting? AddReferenceToFile will search sys.path and load the first file it finds which matches that filename. Depending on where you expect to find the DLL and where it may exist earlier on the path you could be getting a version which you ve compiled earlier. You can also do:

dir(clr.LoadAssemblyFromFile( SprocGenerator.dll ))

to see what types exist in the DLL which you are actually getting back or:

clr.LoadAssemblyFromFile( test.dll ).CodeBase

to see where the file is actually being loaded from.

Check your namespaces. The fact that it complains:

ImportError: No module named Generators

instead of:

ImportError: No module named SprocGenerator.Generators

tells us that it found the SprocGenerator namespace. Is there a misspelling either in C# or Python in the inner namespace, Generators?

Place the assembly in one of the locations specified in sys.path. On my machine:

[ C:Windowssystem32 , C:Program Files (x86)IronPython 2.6Lib , C:Program Files (x86)IronPython 2.6DLLs , C:Program Files (x86)IronPython 2.6 , C:Program Files (x86)IronPython 2.6libsite-packages ]





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签