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.
18 using System.Collections.Generic;
19 using System.Globalization;
20 using System.Reflection;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Binding;
24 using Tizen.NUI.Binding.Internals;
27 namespace Tizen.NUI.EXaml
29 internal class CreateObject : Operation
31 public CreateObject(GlobalDataList globalDataList, List<object> operationInfos)
33 this.typeIndex = (int)operationInfos[0];
34 this.xFactoryMethodIndex = (int)operationInfos[1];
35 if (3 == operationInfos.Count)
37 this.paramList = operationInfos[2] as List<object>;
39 this.globalDataList = globalDataList;
42 private GlobalDataList globalDataList;
43 private int xFactoryMethodIndex;
49 if (0 == globalDataList.GatheredInstances.Count && null != globalDataList.Root)
51 obj = globalDataList.Root;
55 var type = globalDataList.GatheredTypes[typeIndex];
57 var xFactoryMethod = (0 <= xFactoryMethodIndex) ? globalDataList.GatheredMethods[xFactoryMethodIndex] : null;
59 if (null == paramList)
61 if (null == xFactoryMethod)
63 obj = Activator.CreateInstance(type);
67 obj = xFactoryMethod.Invoke(null, Array.Empty<object>());
72 for (int i = 0; i < paramList.Count; i++)
74 if (paramList[i] is Instance instance)
76 paramList[i] = globalDataList.GatheredInstances[instance.Index];
79 if (paramList[i] is IMarkupExtension markupExtension)
81 paramList[i] = markupExtension.ProvideValue(null);
85 if (null == xFactoryMethod)
87 obj = Activator.CreateInstance(type, BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance | BindingFlags.OptionalParamBinding, null, paramList.ToArray(), CultureInfo.CurrentCulture);
91 obj = xFactoryMethod.Invoke(null, paramList.ToArray());
98 globalDataList.GatheredInstances.Add(obj);
100 if (globalDataList.Root == null)
102 globalDataList.Root = obj;
105 if (obj is BindableObject bindableObject)
107 bindableObject.IsCreateByXaml = true;
109 if (1 == globalDataList.GatheredInstances.Count)
111 NameScope nameScope = new NameScope();
112 NameScope.SetNameScope(bindableObject, nameScope);
118 throw new Exception($"Can't create instance typeIndex:{typeIndex}");
122 private int typeIndex;
124 private List<object> paramList;