[NUI] Add IsUsingXaml
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Common / WeakEventHandler.cs
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 using System;
19 using System.Reflection;
20
21 namespace Tizen.NUI
22 {
23     internal sealed class WeakEventHandler<TEventArgs> where TEventArgs : EventArgs
24     {
25         private readonly WeakReference targetReference;
26         private readonly MethodInfo method;
27
28         public WeakEventHandler(EventHandler<TEventArgs> callback)
29         {
30             method = callback.GetMethodInfo();
31             targetReference = new WeakReference(callback.Target, true);
32         }
33
34         public void Handler(object sender, TEventArgs e)
35         {
36             var target = targetReference.Target;
37             if (target != null)
38             {
39                 var callback = (Action<object, TEventArgs>)method.CreateDelegate(typeof(Action<object, TEventArgs>), target);
40                 if (callback != null)
41                 {
42                     callback(sender, e);
43                 }
44             }
45         }
46     }
47 }
48