ko444evnik (ko444evnik) wrote,
ko444evnik
ko444evnik

...какие нынче оказываются моды


internal class EventSink
{
 readonly Type handlerEventArgsType;
 readonly MethodInfo methodInfo;
 readonly WeakReference sink;
 public EventSink(object sink,
          MethodInfo methodInfo)
 {
  this.sink = new WeakReference(sink);
  this.methodInfo = methodInfo;
  ParameterInfo[] parameters = methodInfo.GetParameters();
  if (parameters.Length != 2 || !typeof(EventArgs).IsAssignableFrom(parameters[1].ParameterType))
   throw new ArgumentException("Method does not appear to be a valid event handler", "methodInfo");
  handlerEventArgsType = typeof(EventHandler<>).MakeGenericType(parameters[1].ParameterType);
 }
 public object Sink
 {
  get { return sink.Target; }
 }
 public Exception Invoke(object sender,
             EventArgs e)
 {
  object sinkObject = sink.Target;
  try
  {
   if (sinkObject != null)
   {
    Delegate @delegate = Delegate.CreateDelegate(handlerEventArgsType, sinkObject, methodInfo);
    @delegate.DynamicInvoke(sender, e);
   }
   return null;
  }
  catch (TargetInvocationException ex)
  {
   return ex.InnerException;
  }
 }
}




В такой позе работать с исключениями я еще не пробовал...

источник http://www.gotdotnet.ru/blogs/nesteruk/8226
Tags: c#
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 0 comments