IronPython从C#dll进入内部类

时间:2022-09-02 09:16:53

I'm using IronPython for fetching inner classes from C# dll. for example:

我正在使用IronPython从C#dll中获取内部类。例如:

namespace Platform.CardHost {
    internal class ExtensionManager : IExtensionManager, IDisposable {
    //... other code

IronPython Code

IronPython代码

import clr
clr.AddReference('Platform.CardHost')
import Platform.CardHost.ExtensionManager
# raise ImportError: No module named ExtensionManager
# if it add to ref
clr.AddReference('Platform.CardHost.ExtensionManager')
# raise Error
# IOError: System.IO.IOException: Could not add reference to assembly
# Platform.CardHost.ExtensionManager

How can I import ExtensionManager? Or is this not possible?

如何导入ExtensionManager?或者这不可能吗?

2 个解决方案

#1


2  

So like I already wrote:

就像我已经写过:

make ExtensionManager public if you want to access it from somewhere else than your assembly. The definition of internalis

如果要从程序集以外的其他位置访问它,请将ExtensionManager设为公共。内部的定义

The type or member can be accessed by any code in the same assembly, but not from another assembly.

类型或成员可以由同一程序集中的任何代码访问,但不能从另一个程序集访问。

what you could do, to make it only available for another assembly is, to make it visible for a friend assembly:

你能做什么,让它只适用于另一个组件,是为了让朋友组装可见:

using System.Runtime.CompilerServices;
using System;

[assembly:InternalsVisibleTo("my_friend_assembly")]
internal class ExtensionManager : IExtensionManager, IDisposable 
{
}

else, I don't see any reason why making it internal but trying to access it from another assembly/your ironpython-script. Yeah, for friend assemblies, there are reasons, for sure.

否则,我没有看到为什么让它内部但试图从另一个程序集/你的ironpython脚本访问它的任何理由。是的,对于朋友集会,肯定有理由。

for your new update: "I can't change class":

对于您的新更新:“我无法更改课程”:

so maybe, the guy who wrote that class doesn't want you to import the class from elsewhere? That's the use of internal,protected,private and public.

也许,编写该课程的人不希望你从其他地方导入课程?这是内部,受保护,私人和公共的使用。

Imho, it would be really bad to define in C# a class as internal, so you can't import it from C#, but IronPython still lets you import it.

Imho,将C#中的类定义为内部是非常糟糕的,因此您无法从C#导入它,但IronPython仍允许您导入它。

for sure, you could try getting the code from the assembly, change it and make it again to a new assembly, like you wrote. But that's a lot of work and possibly in the end, it won't work.

当然,您可以尝试从程序集中获取代码,更改它并再次使用新的程序集,就像您写的那样。但这是很多工作,可能最终,它将无法工作。

#2


1  

Thanks to Matthias Burger for a fact that prompted the idea. i try to decompile dll. because the file were large after he disassemble, it can't assemble without problem. I wrote to the guy, he say me use C# interface ICardHost.
here how i use it, maybe for someone who meet similar problem.

感谢Matthias Burger提出了这个想法的事实。我尝试反编译DLL。因为拆解后文件很大,所以无法正常组装。我写信给那个人,他说我用C#接口ICardHost。在这里我如何使用它,也许是遇到类似问题的人。

clr.AddReference('Platform.CardHost')
from Platform import CardHost
from Platform.CardHost import ICardHost

host = CardHost.CardHost.CreateInstance(session)
# ExtensionManager is internal class but it available by interface
# here how to use C# interface
em = ICardHost.ExtensionManager.__get__(host) 

as it in C#

就像在C#中一样

// cardHost
public sealed class CardHost : Component, ICardHost

// ICardHost 
public interface ICardHost {
IExtensionManager ExtensionManager { get; }

#1


2  

So like I already wrote:

就像我已经写过:

make ExtensionManager public if you want to access it from somewhere else than your assembly. The definition of internalis

如果要从程序集以外的其他位置访问它,请将ExtensionManager设为公共。内部的定义

The type or member can be accessed by any code in the same assembly, but not from another assembly.

类型或成员可以由同一程序集中的任何代码访问,但不能从另一个程序集访问。

what you could do, to make it only available for another assembly is, to make it visible for a friend assembly:

你能做什么,让它只适用于另一个组件,是为了让朋友组装可见:

using System.Runtime.CompilerServices;
using System;

[assembly:InternalsVisibleTo("my_friend_assembly")]
internal class ExtensionManager : IExtensionManager, IDisposable 
{
}

else, I don't see any reason why making it internal but trying to access it from another assembly/your ironpython-script. Yeah, for friend assemblies, there are reasons, for sure.

否则,我没有看到为什么让它内部但试图从另一个程序集/你的ironpython脚本访问它的任何理由。是的,对于朋友集会,肯定有理由。

for your new update: "I can't change class":

对于您的新更新:“我无法更改课程”:

so maybe, the guy who wrote that class doesn't want you to import the class from elsewhere? That's the use of internal,protected,private and public.

也许,编写该课程的人不希望你从其他地方导入课程?这是内部,受保护,私人和公共的使用。

Imho, it would be really bad to define in C# a class as internal, so you can't import it from C#, but IronPython still lets you import it.

Imho,将C#中的类定义为内部是非常糟糕的,因此您无法从C#导入它,但IronPython仍允许您导入它。

for sure, you could try getting the code from the assembly, change it and make it again to a new assembly, like you wrote. But that's a lot of work and possibly in the end, it won't work.

当然,您可以尝试从程序集中获取代码,更改它并再次使用新的程序集,就像您写的那样。但这是很多工作,可能最终,它将无法工作。

#2


1  

Thanks to Matthias Burger for a fact that prompted the idea. i try to decompile dll. because the file were large after he disassemble, it can't assemble without problem. I wrote to the guy, he say me use C# interface ICardHost.
here how i use it, maybe for someone who meet similar problem.

感谢Matthias Burger提出了这个想法的事实。我尝试反编译DLL。因为拆解后文件很大,所以无法正常组装。我写信给那个人,他说我用C#接口ICardHost。在这里我如何使用它,也许是遇到类似问题的人。

clr.AddReference('Platform.CardHost')
from Platform import CardHost
from Platform.CardHost import ICardHost

host = CardHost.CardHost.CreateInstance(session)
# ExtensionManager is internal class but it available by interface
# here how to use C# interface
em = ICardHost.ExtensionManager.__get__(host) 

as it in C#

就像在C#中一样

// cardHost
public sealed class CardHost : Component, ICardHost

// ICardHost 
public interface ICardHost {
IExtensionManager ExtensionManager { get; }