使用 PerlNET 将 Perl 对象从不同的 Perl 类返回到 C#
我有两个 Perl 模块,我想将它们作为对象类型公开给 C#。其中一个构造另一种类型的对象并使用如下所示的方法返回它。我在 Type1.dll 中包含对 Type2.dll 的引用,并在 C# 中引用它们。正如代码所示,我可以直接从 C# 构造 Type2 对象,但无法返回由 Type1 中的方法构造的 Type2 对象。有什么想法吗?
(这是从 http://community 交叉发布的.activestate.com/forum/return-perl-object- different-perl-class-c。)
C#:
Type1 obj1 = new Type1(); // Works
Type2 test = new Type2(); // Works
Type2 obj2 = obj1.make2();
// Fails: System.InvalidCastException: Unable to cast object of type
// 'PerlRunTime.SV' to type 'Type2' at Type1.make2()
Perl:Type1.pm
package Type1;
use strict;
use Type2;
=for interface
[interface: pure]
static Type1();
Type2 make2();
=cut
sub new {
my $class = shift;
return bless {}, $class;
}
sub make2 {
my $this = shift;
return Type2->new();
}
1;
Perl:Type2.pm
代码>
package Type2;
use strict;
=for interface
[interface: pure]
static Type2();
=cut
sub new {
my $class = shift;
return bless {}, $class;
}
1;
I have two Perl modules which I want to expose as object types to C#. One of them constructs objects of the other type and returns it using a method as shown below. I'm including a reference to Type2.dll in Type1.dll, and referencing them both in C#. As the code shows I can construct a Type2 object directly from C#, but I can't return a Type2 object that was constructed by a method in Type1. Any ideas?
(This is cross-posted from http://community.activestate.com/forum/return-perl-object-different-perl-class-c.)
C#:
Type1 obj1 = new Type1(); // Works
Type2 test = new Type2(); // Works
Type2 obj2 = obj1.make2();
// Fails: System.InvalidCastException: Unable to cast object of type
// 'PerlRunTime.SV' to type 'Type2' at Type1.make2()
Perl: Type1.pm
package Type1;
use strict;
use Type2;
=for interface
[interface: pure]
static Type1();
Type2 make2();
=cut
sub new {
my $class = shift;
return bless {}, $class;
}
sub make2 {
my $this = shift;
return Type2->new();
}
1;
Perl: Type2.pm
package Type2;
use strict;
=for interface
[interface: pure]
static Type2();
=cut
sub new {
my $class = shift;
return bless {}, $class;
}
1;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于此内容被交叉发布到 community.activestate.com 并已在那里得到答复,我会将答案从那里复制到此处,但我会对其进行修剪,因为我认为它太长了。
主要问题是您编写的方式,
Type2
不被视为类型,并且调用Type2->new()
不会转换为构造函数调用(但静态方法调用)。对代码进行以下更改可修复此问题:
package Type2
更改为package Sample::Type2
。这使得 Type2 成为一个类型,而 Sample 成为一个命名空间。package Type1
更改为package Sample::Type1
。use Type2;
更改为use namespace "Sample";
。这会将 Type2 作为类型导入。在这些更改之后,发布的 C# 代码可以按要求工作。
Since this was cross-posted to community.activestate.com and has been answered there, I'll copy the answer from there to here, but I'll trim it down because I think it's too lengthy.
The main problem is that the way you wrote it,
Type2
is not considered a type, and callingType2->new()
does not translate to a constructor call (but a static method invocation).The following changes to your code fix this:
package Type2
topackage Sample::Type2
. This makes Type2 a type and Sample a namespace.package Type1
topackage Sample::Type1
.use Type2;
touse namespace "Sample";
. This imports Type2 as a type.The posted C# code works as required after these changes.
我还发现我可以创建一个这样的文件:
然后我的 Type1.pm 和 Type2.pm 文件与以前相同,但没有接口 POD。
通过此设置,plc 创建一个包含这两个类的 dll,并且 Type1 类能够创建并返回 Type2 的实例。
这实际上对我来说更方便,因为我的 Type1 和 Type2 类是遗留库的一部分,我希望将其提供给 C# 代码,而不需要对它们进行大量的 .NET 特定更改。我为我的 C# 程序集创建了一个 pm 文件,在其中包含了用于访问遗留库方法的接口定义和一堆 .NET 特定属性方法以及将复杂的 Perl 哈希/数组数据结构返回值转换为Hashtable 和 Array/ArrayList 数据结构。
I've also found that I can create one file like this:
Then my Type1.pm and Type2.pm files are the same as before but without the interface POD.
With this setup, plc creates a single dll that includes both classes, and the Type1 class is able to create and return instances of Type2.
This actually turned out to be more convenient for me, because my Type1 and Type2 classes are part of a legacy library that I want to make available to C# code without making extensive .NET-specific changes to them. I've created one pm file for my C# assembly, and within it I've included the interface definitions for accessing the legacy library methods and a bunch of .NET specific methods for properties and converting complex Perl hash/array data structure return values into Hashtable and Array/ArrayList data structures.