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.Reflection;
21 using Tizen.NUI.BaseComponents;
22 using Tizen.NUI.Binding;
23 using Tizen.NUI.Binding.Internals;
26 namespace Tizen.NUI.EXaml
28 internal class CreateObject : Operation
30 public CreateObject(GlobalDataList globalDataList, List<object> operationInfos)
32 this.typeIndex = (int)operationInfos[0];
33 this.xFactoryMethodIndex = (int)operationInfos[1];
34 if (3 == operationInfos.Count)
36 this.paramList = operationInfos[2] as List<object>;
38 this.globalDataList = globalDataList;
41 private GlobalDataList globalDataList;
42 private int xFactoryMethodIndex;
48 if (0 == globalDataList.GatheredInstances.Count && null != globalDataList.Root)
50 obj = globalDataList.Root;
54 var type = globalDataList.GatheredTypes[typeIndex];
56 var xFactoryMethod = (0 <= xFactoryMethodIndex) ? globalDataList.GatheredMethods[xFactoryMethodIndex] : null;
58 if (null == paramList)
60 if (null == xFactoryMethod)
62 obj = Activator.CreateInstance(type);
66 obj = xFactoryMethod.Invoke(null, Array.Empty<object>());
71 for (int i = 0; i < paramList.Count; i++)
73 if (paramList[i] is Instance instance)
75 paramList[i] = globalDataList.GatheredInstances[instance.Index];
78 if (paramList[i] is IMarkupExtension markupExtension)
80 paramList[i] = markupExtension.ProvideValue(null);
84 if (null == xFactoryMethod)
86 obj = Activator.CreateInstance(type, paramList.ToArray());
90 obj = xFactoryMethod.Invoke(null, paramList.ToArray());
97 globalDataList.GatheredInstances.Add(obj);
99 if (globalDataList.Root == null)
101 globalDataList.Root = obj;
104 if (obj is BindableObject bindableObject)
106 bindableObject.IsCreateByXaml = true;
108 if (1 == globalDataList.GatheredInstances.Count)
110 NameScope nameScope = new NameScope();
111 NameScope.SetNameScope(bindableObject, nameScope);
117 throw new Exception($"Can't create instance typeIndex:{typeIndex}");
121 private int typeIndex;
123 private List<object> paramList;