[NUI][Xaml] Support create element in Xaml with default params
authorFang Xiaohui <xiaohui.fang@samsung.com>
Thu, 17 Jun 2021 06:40:16 +0000 (14:40 +0800)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Wed, 23 Jun 2021 08:25:09 +0000 (17:25 +0900)
src/Tizen.NUI/src/internal/Xaml/CreateValuesVisitor.cs

index 772ed8b..b0e4c19 100755 (executable)
@@ -114,8 +114,50 @@ namespace Tizen.NUI.Xaml
                         }
                         else
                         {
+                            ConstructorInfo constructorInfo = null;
+
                             //constructor with all default parameters
-                            value = Activator.CreateInstance(type, BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance | BindingFlags.OptionalParamBinding, null, new object[] { Type.Missing }, CultureInfo.CurrentCulture);
+                            foreach (var constructor in type.GetConstructors())
+                            {
+                                if (!constructor.IsStatic)
+                                {
+                                    bool areAllParamsDefault = true;
+
+                                    foreach (var param in constructor.GetParameters())
+                                    {
+                                        if (!param.HasDefaultValue)
+                                        {
+                                            areAllParamsDefault = false;
+                                            break;
+                                        }
+                                    }
+
+                                    if (areAllParamsDefault)
+                                    {
+                                        if (null == constructorInfo)
+                                        {
+                                            constructorInfo = constructor;
+                                        }
+                                        else
+                                        {
+                                            throw new XamlParseException($"{type.FullName} has more than one constructor which params are all default.", node);
+                                        }
+                                    }
+                                }
+                            }
+
+                            if (null == constructorInfo)
+                            {
+                                throw new XamlParseException($"{type.FullName} has no constructor which params are all default.", node);
+                            }
+
+                            List<object> defaultParams = new List<object>();
+                            foreach (var param in constructorInfo.GetParameters())
+                            {
+                                defaultParams.Add(param.DefaultValue);
+                            }
+
+                            value = Activator.CreateInstance(type, defaultParams.ToArray());
                         }
                         if (value is Element)
                         {