本文整理汇总了C#中System.Reflection.EventInfo.AddEventHandler方法的典型用法代码示例。如果您正苦于以下问题:C# EventInfo.AddEventHandler方法的具体用法?C# EventInfo.AddEventHandler怎么用?C# EventInfo.AddEventHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.EventInfo
的用法示例。
在下文中一共展示了EventInfo.AddEventHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Monitor
/// <summary>
/// Creates an Event Handler for the EventInfo Passed to Hook it the Event Up to an Event Recorder.
/// </summary>
private static EventRecorder Monitor(object eventSource, EventInfo eventInfo)
{
// Create EventRecorder
var eventRecorder = new EventRecorder(eventSource, eventInfo.Name);
// Subscribe EventRecorder to event
var handler = EventSubscriber.GenerateHandler(eventInfo.EventHandlerType, eventRecorder);
eventInfo.AddEventHandler(eventSource, handler);
return eventRecorder;
}
示例2: WireEvent
/// <summary>
/// Wires the event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="event">The event.</param>
/// <param name="handler">The handler.</param>
public static void WireEvent(object sender, EventInfo @event, Action<object, EventArgs> handler)
{
@event.AddEventHandler(
sender,
Delegate.CreateDelegate(@event.EventHandlerType, handler.Target, handler.Method)
);
}
示例3: DomEventInstance
public DomEventInstance(DomNodeInstance node, EventInfo eventInfo)
{
Getter = new ClrFunctionInstance(node.Engine, (thisObject, arguments) => _function ?? JsValue.Null);
Setter = new ClrFunctionInstance(node.Engine, (thisObject, arguments) =>
{
if (_handler != null)
{
eventInfo.RemoveEventHandler(node.Value, _handler);
_handler = null;
_function = null;
}
if (arguments[0].Is<FunctionInstance>())
{
_function = arguments[0].As<FunctionInstance>();
_handler = (s, ev) =>
{
var sender = s.ToJsValue(node.Context);
var args = ev.ToJsValue(node.Context);
_function.Call(sender, new [] { args });
};
eventInfo.AddEventHandler(node.Value, _handler);
}
return arguments[0];
});
}
示例4: Bind
/// <summary>
/// Adds specified <paramref name="listenerMethod"></paramref> as an event handler for
/// the <paramref name="sourceEvent"></paramref>.
/// </summary>
/// <param name="sourceEvent">Event on source object to which <paramref name="listenerMethod"></paramref> will be added.</param>
/// <param name="listenerMethod">Method to be added as event handler for <paramref name="listenerMethod"></paramref>.</param>
public virtual void Bind(EventInfo sourceEvent, MethodInfo listenerMethod)
{
if (sourceEvent == null) throw new ArgumentNullException("sourceEvent");
Delegate methodToBindTo =
Delegate.CreateDelegate(sourceEvent.EventHandlerType, listener, listenerMethod);
sourceEvent.AddEventHandler(source, methodToBindTo);
}
示例5: EventPropegator
public EventPropegator(object instance, EventInfo evt, Action<object, EventArgs> handler)
{
_instance = instance;
_evt = evt;
_disposed = false;
var methodInfo = handler.GetType().GetMethod("Invoke");
_delegate = Delegate.CreateDelegate(evt.EventHandlerType, handler, methodInfo);
evt.AddEventHandler(instance, _delegate);
}
示例6: AddPublisher
public void AddPublisher(string[] topics, object o, EventInfo evt)
{
if (o == null)
{
throw new ArgumentNullException ("o");
}
if (evt == null)
{
throw new ArgumentNullException ("evt");
}
var pub = new Publication {Topics = topics, Broker = this};
var dg = Delegate.CreateDelegate (typeof(Action), pub, _pubmethod);
evt.AddEventHandler (o, dg);
}
示例7: AddPublication
/// <summary>
///
/// </summary>
/// <param name="instance"></param>
/// <param name="eventInfo"></param>
public void AddPublication( object instance, EventInfo eventInfo )
{
Delegate method = Delegate.CreateDelegate( eventInfo.EventHandlerType, this, BroadcastMethod );
eventInfo.AddEventHandler( instance, method );
lock ( _publications )
{
var publication = new Publication
{
Method = method,
Instance = new WeakReference( instance ),
Event = eventInfo
};
_publications.Add( publication );
}
}
示例8: AttachGeneralHandler
// 原函数
public static void AttachGeneralHandler(object target, EventInfo targetEvent)
{
//获得事件响应程序的委托类型
var delegateType = targetEvent.EventHandlerType;
//这个委托的Invoke方法有我们所需的签名信息
MethodInfo invokeMethod = delegateType.GetMethod("Invoke");
//按照这个委托制作所需要的参数
ParameterInfo[] parameters = invokeMethod.GetParameters();
ParameterExpression[] paramsExp = new ParameterExpression[parameters.Length];
Expression[] argsArrayExp = new Expression[parameters.Length];
//参数一个个转成object类型。有些本身即是object,管他呢……
for (int i = 0; i < parameters.Length; i++)
{
paramsExp[i] = Expression.Parameter(parameters[i].ParameterType, parameters[i].Name);
argsArrayExp[i] = Expression.Convert(paramsExp[i], typeof(Object));
}
//调用我们的GeneralHandler
MethodInfo executeMethod = typeof(GeneralEventHandling).GetMethod(
"GeneralHandler", BindingFlags.Static | BindingFlags.NonPublic);
Expression lambdaBodyExp =
Expression.Call(null, executeMethod, Expression.NewArrayInit(typeof(Object), argsArrayExp));
//如果有返回值,那么将返回值转换成委托要求的类型
//如果没有返回值就这样搁那里就成了
if (!invokeMethod.ReturnType.Equals(typeof(void)))
{
//这是有返回值的情况
lambdaBodyExp = Expression.Convert(lambdaBodyExp, invokeMethod.ReturnType);
}
//组装到一起
LambdaExpression dynamicDelegateExp = Expression.Lambda(delegateType, lambdaBodyExp, paramsExp);
//我们创建的Expression是这样的一个函数:
//(委托的参数们) => GeneralHandler(new object[] { 委托的参数们 })
//编译
Delegate dynamiceDelegate = dynamicDelegateExp.Compile();
//完成!
targetEvent.AddEventHandler(target, dynamiceDelegate);
}
示例9: AddEventHandler
public static void AddEventHandler(EventInfo eventInfo, object item, Action<object, EventArgs> action)
{
var parameters = eventInfo.EventHandlerType
.GetMethod("Invoke")
.GetParameters()
.Select(parameter => Expression.Parameter(parameter.ParameterType))
.ToArray();
var invoke = action.GetType().GetMethod("Invoke");
var handler = Expression.Lambda(
eventInfo.EventHandlerType,
Expression.Call(Expression.Constant(action), invoke, parameters[0], parameters[1]),
parameters
)
.Compile();
eventInfo.AddEventHandler(item, handler);
}
示例10: RegisterEventHandler
public static AnonymousEventHandler RegisterEventHandler(EventInfo eventInfo, object target, Action handler)
{
ParameterInfo[] parameterInfos = eventInfo.EventHandlerType.GetMethod("Invoke").GetParameters();
if (parameterInfos.Length != 2)
throw new ArgumentException("The given event info must have exactly two parameters.");
Type argumentType = parameterInfos.Skip(1).First().ParameterType;
Type type = typeof(AnonymousEventHandler<>).MakeGenericType(argumentType);
MethodInfo method = type.GetMethod("Handler");
var anonymousHandler = (AnonymousEventHandler)Activator.CreateInstance(type);
anonymousHandler.Action = handler;
anonymousHandler.eventHandler = Delegate.CreateDelegate(eventInfo.EventHandlerType, anonymousHandler, method);
anonymousHandler.eventInfo = eventInfo;
anonymousHandler.target = target;
eventInfo.AddEventHandler(target, anonymousHandler.eventHandler);
return anonymousHandler;
}
示例11: AddEventHandler
internal void AddEventHandler(DependencyObject owner)
{
_owner = owner;
_eventInfo = owner.GetType().GetEvent(Event);
if(_eventInfo == null)
{
throw new ArgumentException("The given event does not exist for the element.", "Event");
}
_eventHandler = EventBuilder.Create(_eventInfo,
() =>
{
if (Command.CanExecute(Parameter))
{
Command.Execute(Parameter);
}
});
_eventInfo.AddEventHandler(owner, _eventHandler);
}
示例12: CommandBinding
/// <summary>
/// Initializes a new instance of the <see cref="CommandBinding"/> class.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="eventName">Name of the event.</param>
/// <param name="command">The command.</param>
/// <param name="commandParameterBinding">The command parameter binding.</param>
/// <exception cref="ArgumentNullException">The <paramref name="element"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">The <paramref name="eventName"/> is <c>null</c> or whitespace.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="command"/> is <c>null</c>.</exception>
public CommandBinding(object element, string eventName, ICatelCommand command, Binding commandParameterBinding = null)
{
Argument.IsNotNull("element", element);
Argument.IsNotNullOrWhitespace("eventName", eventName);
Argument.IsNotNull("command", command);
_element = element;
_command = command;
_commandParameterBinding = commandParameterBinding;
var elementType = _element.GetType();
_eventInfo = elementType.GetEventEx(eventName);
if (_eventInfo == null)
{
throw Log.ErrorAndCreateException<InvalidOperationException>("Event '{0}.{1}' not found, cannot create command binding", elementType.Name, eventName);
}
_enabledPropertyInfo = elementType.GetPropertyEx("Enabled");
_eventHandler = delegate
{
var commandParameter = _commandParameterBinding.GetBindingValue();
if (_command.CanExecute(commandParameter))
{
_command.Execute(commandParameter);
}
};
_eventInfo.AddEventHandler(element, _eventHandler);
_canExecuteChangedHandler = (sender, e) => UpdateEnabledState();
command.CanExecuteChanged += _canExecuteChangedHandler;
if (commandParameterBinding != null)
{
_commandBindingParameterValueChangedHandler = (sender, e) => UpdateEnabledState();
commandParameterBinding.ValueChanged += _commandBindingParameterValueChangedHandler;
}
UpdateEnabledState();
}
示例13: CustomDataContextSetup
public CustomDataContextSetup(Type entityType)
{
Debug.Assert(entityType != null);
this.entityType = entityType;
this.dataServiceType = typeof(TypedCustomDataContext<>).MakeGenericType(entityType);
this.handler = new EventHandler(TypedDataContextValuesRequested);
this.valuesRequestedEvent = this.dataServiceType.GetEvent("ValuesRequested", BindingFlags.Static | BindingFlags.Public);
Assert.IsNotNull(valuesRequestedEvent, "valuesRequestedEvent is not null");
valuesRequestedEvent.AddEventHandler(this, handler);
memberPropertyInfo = entityType.GetProperty("Member");
if (entityType.GetGenericTypeDefinition() == typeof(TypedEntity<,>))
{
idPropertyInfo = entityType.GetProperty("ID");
}
else
{
Debug.Assert(entityType.GetGenericTypeDefinition() == typeof(DoubleKeyTypedEntity<,,>));
idPropertyInfo = entityType.GetProperty("FirstKey");
secondIdPropertyInfo = entityType.GetProperty("SecondKey");
}
}
示例14: AddEventHandler
public static void AddEventHandler(EventInfo eventinfo, object target, IApply func)
{
var type = eventinfo.EventHandlerType;
var dlg = ConvertToDelegate(type, func);
eventinfo.AddEventHandler(target, dlg);
}
示例15: Bind
public virtual void Bind(EventInfo sourceEvent, MethodInfo listenerMethod)
{
Delegate handler = Delegate.CreateDelegate(sourceEvent.EventHandlerType, this.listener, listenerMethod);
sourceEvent.AddEventHandler(this.source, handler);
}