a5477eb8cb9691d7a968a93b19214d41513f9bc0
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / EXaml / Operation / CreateObject.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 using System;
18 using System.Collections.Generic;
19 using System.Globalization;
20 using System.Reflection;
21 using System.Text;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Binding;
24 using Tizen.NUI.Binding.Internals;
25 using Tizen.NUI.Xaml;
26
27 namespace Tizen.NUI.EXaml
28 {
29     internal class CreateObject : Operation
30     {
31         public CreateObject(GlobalDataList globalDataList, List<object> operationInfos)
32         {
33             this.typeIndex = (int)operationInfos[0];
34             this.xFactoryMethodIndex = (int)operationInfos[1];
35             if (3 == operationInfos.Count)
36             {
37                 this.paramList = operationInfos[2] as List<object>;
38             }
39             this.globalDataList = globalDataList;
40         }
41
42         private GlobalDataList globalDataList;
43         private int xFactoryMethodIndex;
44
45         public void Do()
46         {
47             object obj = null;
48
49             if (0 == globalDataList.GatheredInstances.Count && null != globalDataList.Root)
50             {
51                 obj = globalDataList.Root;
52             }
53             else
54             {
55                 var type = globalDataList.GatheredTypes[typeIndex];
56
57                 var xFactoryMethod = (0 <= xFactoryMethodIndex) ? globalDataList.GatheredMethods[xFactoryMethodIndex] : null;
58
59                 if (null == paramList)
60                 {
61                     if (null == xFactoryMethod)
62                     {
63                         obj = Activator.CreateInstance(type);
64                     }
65                     else
66                     {
67                         obj = xFactoryMethod.Invoke(null, Array.Empty<object>());
68                     }
69                 }
70                 else
71                 {
72                     for (int i = 0; i < paramList.Count; i++)
73                     {
74                         if (paramList[i] is Instance instance)
75                         {
76                             paramList[i] = globalDataList.GatheredInstances[instance.Index];
77                         }
78
79                         if (paramList[i] is IMarkupExtension markupExtension)
80                         {
81                             paramList[i] = markupExtension.ProvideValue(null);
82                         }
83                     }
84
85                     if (null == xFactoryMethod)
86                     {
87                         obj = Activator.CreateInstance(type, BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance | BindingFlags.OptionalParamBinding, null, paramList.ToArray(), CultureInfo.CurrentCulture);
88                     }
89                     else
90                     {
91                         obj = xFactoryMethod.Invoke(null, paramList.ToArray());
92                     }
93                 }
94             }
95
96             if (null != obj)
97             {
98                 globalDataList.GatheredInstances.Add(obj);
99
100                 if (globalDataList.Root == null)
101                 {
102                     globalDataList.Root = obj;
103                 }
104
105                 if (obj is BindableObject bindableObject)
106                 {
107                     bindableObject.IsCreateByXaml = true;
108
109                     if (1 == globalDataList.GatheredInstances.Count)
110                     {
111                         NameScope nameScope = new NameScope();
112                         NameScope.SetNameScope(bindableObject, nameScope);
113                     }
114                 }
115             }
116             else
117             {
118                 throw new Exception($"Can't create instance typeIndex:{typeIndex}");
119             }
120         }
121
122         private int typeIndex;
123
124         private List<object> paramList;
125     }
126 }