2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Reflection;
23 internal sealed class WeakEventHandler<TEventArgs> where TEventArgs : EventArgs
25 private readonly WeakReference targetReference;
26 private readonly MethodInfo method;
28 public WeakEventHandler(EventHandler<TEventArgs> callback)
30 method = callback.GetMethodInfo();
31 targetReference = new WeakReference(callback.Target, true);
34 public void Handler(object sender, TEventArgs e)
36 var target = targetReference.Target;
39 var callback = (Action<object, TEventArgs>)method.CreateDelegate(typeof(Action<object, TEventArgs>), target);