[EXaml] Refactor EXaml
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / EXaml / Operation / AddEvent.cs
index fadecf8..a444926 100755 (executable)
@@ -1,4 +1,20 @@
-using System;
+/*
+ * Copyright(c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+using System;
 using System.Collections.Generic;
 using System.Reflection;
 using System.Text;
@@ -10,23 +26,45 @@ namespace Tizen.NUI.EXaml
 {
     internal class AddEvent : Operation
     {
-        public AddEvent(int instanceIndex, int elementIndex, int eventIndex, int valueIndex)
+        public AddEvent(GlobalDataList globalDataList, List<object> operationInfo)
         {
-            this.instanceIndex = instanceIndex;
-            this.elementIndex = elementIndex;
-            this.eventIndex = eventIndex;
-            this.valueIndex = valueIndex;
+            instanceIndex = (int)operationInfo[0];
+            elementIndex = (int)operationInfo[1];
+            eventIndex = (int)operationInfo[2];
+            valueIndex = (int)operationInfo[3];
+            this.globalDataList = globalDataList;
         }
 
+        private GlobalDataList globalDataList;
+
         public void Do()
         {
-            object instance = LoadEXaml.GatheredInstances[instanceIndex];
-            object element = LoadEXaml.GatheredInstances[elementIndex];
-            var eventInfo = GatherEvent.GatheredEvents[eventIndex];
+            object instance = globalDataList.GatheredInstances[instanceIndex];
+            object element = globalDataList.GatheredInstances[elementIndex];
+            var eventInfo = globalDataList.GatheredEvents[eventIndex];
             try
             {
-                var methodInfo = GatherMethod.GatheredMethods[valueIndex];
-                eventInfo.AddEventHandler(instance, methodInfo.CreateDelegate(eventInfo.EventHandlerType, element));
+                var methodInfo = globalDataList.GatheredMethods[valueIndex];
+                Delegate eventDelegate = null;
+
+                if (methodInfo.IsStatic)
+                {
+                    eventDelegate = methodInfo.CreateDelegate(eventInfo.EventHandlerType);
+                }
+                else
+                {
+                    eventDelegate = methodInfo.CreateDelegate(eventInfo.EventHandlerType, element);
+                }
+
+                if (null != eventDelegate)
+                {
+                    eventInfo.AddEventHandler(instance, eventDelegate);
+                    globalDataList.RemoveEventOperations.Add(new RemoveEvent(eventDelegate, instance, eventInfo));
+                }
+                else
+                {
+                    throw new Exception($"Can't create delegate for method {methodInfo.DeclaringType.FullName}.{methodInfo.Name}");
+                }
             }
             catch (ArgumentException ae)
             {