.Net 包装器事件处理

.Net wrapper eventhandling

情况:
我有一个 dll 和一个程序,应该使用这个 dll 和后期绑定。

每个方法都有效也是事件。例如,有一个日志记录事件。我想在程序中使用自己的日志记录方法,但这是我的问题。我不知道。

dll 日志 class (c#):

/// <summary>
/// log helper class.
/// </summary>
public static class Log {

    // ######### Enum #############################################################################################

    /// <summary>
    /// Log level enum.
    /// </summary>
    public enum LogLevel {
        None = -1,
        Debug = 0,
        Info = 1,
        Warn = 2,
        Error = 3,
        Fatal = 4
    }


    // ######### Events ###########################################################################################

    /// <summary>
    /// log helper class log event.
    /// </summary>
    public static event EventHandler<HelperLogEventArgs> HelperLog;


    // ######### Constructor ######################################################################################

    /// <summary>
    /// Default constructor
    /// </summary>
    static Log() {
        HelperLog += DebugLog;
    }


    // ######### Methods ##########################################################################################

    /// <summary>
    /// log helper class log event handling.
    /// </summary>
    /// <param name="e">Log event arguments</param>
    public static void OnHelperLog(HelperLogEventArgs e) {
        EventHandler<HelperLogEventArgs> handler = HelperLog;
        if (handler != null) {
            handler(null, e);
        }
    }


    /// <summary>
    /// Log a message to the debug console.
    /// </summary>
    /// <param name="sender">Event sender</param>
    /// <param name="e">Log event arguments</param>
    private static void DebugLog(object sender, HelperLogEventArgs e) {
        Debug.WriteLine(String.Format(" [{0}]: {1}", e.logLevel.ToString(), e.message));
    }


    /// <summary>
    /// Log a message.
    /// </summary>
    /// <param name="level">Log level</param>
    /// <param name="message">Log message</param>
    public static void Log(LogLevel level, string message) {
        HelperLogEventArgs args = new HelperLogEventArgs();
        args.logLevel = level;
        args.logTime = DateTime.Now;
        args.message = message;
        OnHelperLog(args);
    }


    // ######### Help Classes #####################################################################################

    /// <summary>
    /// log helper class event argument class.
    /// </summary>
    public class HelperLogEventArgs : EventArgs {
        /// <summary>
        /// Log message log level.
        /// </summary>
        public Log.LogLevel logLevel;

        /// <summary>
        /// Log message text.
        /// </summary>
        public string message;

        /// <summary>
        /// Log message log timestamp.
        /// </summary>
        public DateTime logTime;
    }

}

现在我在外部程序中有一个 Wrapper class,不幸的是用另一种语言加载了 dll (vb.Net):

If _oAssembly Is Nothing Then
    _oAssembly = Assembly.LoadFrom(sTfsHelperDllPath)
End If

' This will not throw an exception but return nothing.
_oTFSHelper = _oAssembly.CreateInstance("TFSHelper.TFSHelper")
If _oTFSHelper Is Nothing Then
    ' Throw manually an exception on error.
    Throw New Exception("## TFSHelper could not be loaded. ##")
End If

现在有人可以告诉我如何在外部程序中编写和添加日志记录方法/事件处理程序并调用 will 然后引发 ddl 中的事件吗?

一种可能的解决方案是使用反射来添加事件:

' Add event Handler
Dim method As MethodInfo = Me.GetType().GetMethod("<NewEventHandlerName>", BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.Public)
Dim eventInfo As EventInfo = _oTFSHelper.GetType().GetEvent("<EventName>")
Dim type As Type = eventInfo.EventHandlerType
Dim handler As [Delegate] = [Delegate].CreateDelegate(type, Me, method)
eventInfo.AddEventHandler(_oTFSHelper, handler)