【发布时间】:2013-01-22 08:36:00
【问题描述】:
编辑:我找到了关于 C# 和 COM 的相关页面:How does the C# compiler detect COM types?
如标题所示,当我将 C# 程序转换为 IronPython 时,我无法创建类的实例。
C#原程序:IAgilent34980A2 host = new Agilent34980A();
重写的 IronPython 程序:host = Agilent34980A()
C# 程序运行良好,而 IronPython 程序出现以下错误:
TypeError:无法创建 Agilent34980A 的实例,因为它是抽象的
其实Agilent34980A()是一个接口,所以报错是合理的。
我的问题是 为什么它在 C# 中有效?实例也不能在 C# 中创建,它是一个接口,对吧?
加法:
C# 代码来自测试机标记。
IAgilent34980A2源代码定义部分如下:
使用 Ivi.Driver.Interop;
使用系统;
使用 System.Runtime.InteropServices;
命名空间 Agilent.Agilent34980A.Interop
{
// IAgilent34980A interface. [TypeLibType(256)] [Guid("07678A7D-048A-42A6-8884-6CC8C575BD1F")] [InterfaceType(1)] public interface IAgilent34980A2 : IIviDriver { IAgilent34980AMeasurement Measurement { get; } IAgilent34980AVoltage Voltage { get; } // There are some similar functions following. }}
Agilent34980A 定义部分
使用 System.Runtime.InteropServices;
命名空间 Agilent.Agilent34980A.Interop
{
// Agilent34980A driver class. [Guid("07678A7D-048A-42A6-8884-6CC8C575BD1F")] [CoClass(typeof(Agilent34980AClass))] public interface Agilent34980A : IAgilent34980A2 { }}
和IIviDriver定义部分
使用系统;
使用 System.Runtime.InteropServices;
命名空间 Ivi.Driver.Interop
{
// IVI Driver root interface [TypeLibType(256)] [InterfaceType(1)] [Guid("47ED5184-A398-11D4-BA58-000064657374")] public interface IIviDriver { // Pointer to the IIviDriverOperation interface [DispId(1610678272)] IIviDriverOperation DriverOperation { get; } // Pointer to the IIviDriverIdentity interface [DispId(1610678273)] IIviDriverIdentity Identity { get; } // There are some similar functions following. }
【问题讨论】:
-
IronPython 是否支持接口..?听起来您需要寻找 IronPython 专家
-
谢谢你,DJ KRAZE。正如你所说,我应该找一个 IronPython 专家。但在此之前,我真的不明白为什么它在 C# 中运行良好,虽然 Agilent34980() 是一个接口。
-
How to use a specific interface on a C# object in IronPython 看看这个,也许你可以使用这个例子来解决你的情况
-
你能再检查一下你的代码吗?您的问题使用
IAgilent34980A、Agilent34980和Agilent34980A,我不确定这是问题的一部分还是只是错字。列出这三种类型在 c# 中的定义。 -
基于通用命名约定 IAgilent34980A host = new Agilent34980(); IAgilent34980A --> 接口 Agilent34980 --> 实现该接口的类。这在 C# 中是完全合法的。您不是在创建接口对象,而是创建实现该接口的类的对象
标签: c# com ironpython