在 C# 中未调用委托方法

Delegate method not invoked in C#

我正在尝试调用委托来验证来自 FTP 服务器的传入 SSL 证书。

当我在 OnCertificateReceived 函数中放置一个调试点时,执行从未停止,并且 SSL 证书从未被验证。

如果我在这里做错了什么,请有人指出。

class Program
{

var hostname = '';
var username = '';
var password = '';

public static void main()
{
  new Program().XceedFtpWithSSL();
}

void XceedFtpWithSSL()
{
connection = new FtpConnection(hostname,21,username,password,AuthenticationMethod.TlsAuto,VerificationFlags.None,null,DataChannelProtection.Private,false);

connection.CertificateReceived += new CertificateReceivedEventHandler( this.OnCertificateReceived );
}

// When I put debug point in this method, execution never stops

private void OnCertificateReceived(object sender, CertificateReceivedEventArgs e)
        {
            // The Status argument property tells you if the server certificate was accepted
            // based on the VerificationFlags you provided.
            if (e.Status != VerificationStatus.ValidCertificate)
            {

                Console.WriteLine("Do you want to accept this certificate anyway? [Y/N]");
                int answer = Console.Read();

                if ((answer == 'y') || (answer == 'Y'))
                {
                    e.Action = VerificationAction.Accept;
                }
            }
            else
            {
                Console.WriteLine("Valid certificate received from server.");
                // e.Action's default value is VerificationAction.Accept
            }
        } // End of Delegate

}//End of class

那么你正在向委托应用一个事件,为了让他执行 defato 必须在 FtpConnection 中有一些函数来执行这个委托事件:Ex

public delegate void ExDelegate(string value);

class Program
{
    static void Main(string[] args)
    {
        Connection connection = new Connection();
        connection.ExDelegate += OnConnection_ExDelegate;
        connection.Init();
    }

    public static void OnConnection_ExDelegate(string value)
    {
        Console.WriteLine(value);
        Console.ReadLine();
    }
}

public class Connection
{
    public event ExDelegate ExDelegate;

    public void Init()
    {
        Console.Write("Enter your name: ");
        ExDelegate?.Invoke(Console.ReadLine());
    }
}