![](/img/trans.png)
[英]How do I raise an event in FakeItEasy for an event based on a custom delegate?
[英]How do I raise an event in a PerlNet module?
我使用 PDK 7.3 作為開發工具,用於開發同時使用 .NET 和 Perl 的應用程序。
我想將 Perl 構建到與 .NET 兼容的 dll 中,並且此模塊可以引發 Z303CB0EF9EDB9082AZD61BBBE58 應用程序可以處理的事件。
我找不到任何關於它的文檔。 據我所知,PDK 7.3 不支持實現委托。
這是真的? 我有什么選擇嗎?
我給 ActiveState 公司的 Jan Dubois 寫了一封 email。 他提出了一種實現回調機制的方法。
希望這有幫助。
============================================
嗨,明,
您可以在 PerlNET 中很好地實現委托,只是不能在其中聲明新的委托類型。
我附上了一些在 Perl 中定義委托的示例代碼,然后將其傳遞給 C#。 您需要將這些部分分成單獨的文件並手動編譯它們; 該代碼是從一些回歸測試中刪除的,並且您將沒有測試工具的 rest 來按原樣運行它。 (看起來格式也被砍了一點,但代碼的意圖應該還是很清楚的)。
干杯,
-一月
#!./perl -w
# Define some properties and fields in perl and try to access them.
print "1..1\n";
require 'setup.pl';
csc("-target:library DB.cs");
cleanup("DB.dll");
plc(-target => "library", -out => "P.dll", -r => "DB.dll", "P.pm");
csc("test.cs -r:DB.dll -r:P.dll");
cleanup("./test.exe");
my $res = `./test.exe`;
print "----\n$res----\n";
print "not " unless $res eq <<'EOT';
XXX 65
XXX 66
XXX 67
XXX 68
XXX 69
XXX 70
XXX 71
XXX 4.2
XXX 4.30000019073486
XXX 4.4
XXX a
XXX 1
XXX ""
XXX Hi
XXX <undef>
EOT
print "ok 1\n";
__END__
======================= test.cs =====================
using System;
class main : IPrint
{
public static void Main(string[] args)
{
P p = new P(new main());
DataBase db = new DataBase();
db.Add((byte)65);
db.Add((short)66);
db.Add((int)67);
db.Add((long)68);
db.Add((uint)69);
db.Add((ushort)70);
db.Add((ulong)71);
db.Add(4.2D);
db.Add(4.3F);
db.Add(4.4M);
db.Add('a');
db.Add(true);
db.Add(false);
db.Add("Hi");
db.Add(null);
db.Scan(p.handler);
}
main() {}
public void print(string s) {
Console.WriteLine(s);
}
}
======================= DB.cs =====================
using System;
using System.Collections;
public delegate void ProcessItem(Object i);
public class DataBase : ArrayList {
public DataBase() {}
public void Scan(ProcessItem handler) {
foreach (object o in this) {
handler(o);
}
}
}
public interface IPrint {
void print(string s);
}
=======================P.pm =====================
package P;
=for interface
interface ProcessItem; # a delegate type
interface IPrint;
interface System.Object;
interface P {
static P P(IPrint ip);
readonly ProcessItem handler;
void x(System.Object y);
private field IPrint ip;
}
=cut
sub P {
my($self, $ip) = @_;
$self->{ip} = $ip;
}
sub handler {
my $self = shift;
return ProcessItem->new($self, "x");
}
sub x {
my($self, $obj) = @_;
$obj = "<undef>" unless defined $obj;
$obj = '""' unless length $obj;
$self->{ip}->print("XXX $obj");
}
1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.