private static string MakeCodeGetFromControls(int mdiChildrenIndex, Control root, Type type, List <int> next, out bool nogood)
        {
            nogood = false;
            var decendants = new List <Control>();

            WindowUtilityInTarget.GetDecendants(root, decendants);
            int cnt = 0;

            foreach (var e in decendants)
            {
                if (type.IsAssignableFrom(e.GetType()))
                {
                    cnt++;
                }
            }
            if (cnt == 1)
            {
                return($"Core.IdentifyFromTypeFullName(\"{type.FullName}\")");
            }

            nogood = true;
            var code = "Core.Dynamic()";

            if (mdiChildrenIndex != -1)
            {
                code += $".MdiChildren[{mdiChildrenIndex}]";
            }
            foreach (var i in next)
            {
                code += $".Controls[{i}]";
            }
            return(code);
        }
        private string GenerateCode(Control root, Control targetControl, string nameSpace, string driverClassName, List <string> usings, List <string> members, List <Type> getFromControlTreeOnly)
        {
            var code = new List <string>
            {
                "using Codeer.Friendly.Dynamic;",
                "using Codeer.Friendly.Windows;",
                "using Codeer.Friendly.Windows.Grasp;",
                "using Codeer.TestAssistant.GeneratorToolKit;"
            };

            foreach (var e in usings)
            {
                code.Add($"using {e};");
            }
            int nextUsingIndex = code.Count;

            var existMany = false;

            if (getFromControlTreeOnly.Contains(targetControl.GetType()))
            {
                int checkCount = 0;
                existMany = WindowUtilityInTarget.ExistMany(root, targetControl.GetType(), ref checkCount);
                if (existMany)
                {
                    code.Add("using System.Linq;");
                }
            }

            var attr = (targetControl is Form) ? "WindowDriver" : "UserControlDriver";

            code.Add(string.Empty);
            code.Add($"namespace {nameSpace}");
            code.Add("{");
            code.Add($"{Indent}[{attr}(TypeFullName = \"{targetControl.GetType().FullName}\")]");
            code.Add($"{Indent}public class {driverClassName}");
            code.Add($"{Indent}{{");
            code.Add($"{Indent}{Indent}public WindowControl Core {{ get; }}");
            foreach (var e in members)
            {
                code.Add($"{Indent}{Indent}{e}");
            }
            code.Add(string.Empty);
            code.Add($"{Indent}{Indent}public {driverClassName}(WindowControl core)");
            code.Add($"{Indent}{Indent}{{");
            code.Add($"{Indent}{Indent}{Indent}Core = core;");
            code.Add($"{Indent}{Indent}}}");
            code.Add($"{Indent}}}");

            if (getFromControlTreeOnly.Contains(targetControl.GetType()))
            {
                code.Add(string.Empty);
                code.Add($"{Indent}public static class {driverClassName}_Extensions");
                code.Add($"{Indent}{{");
                var funcName   = GetFuncName(driverClassName);
                var rootDriver = _driverTypeNameManager.MakeDriverType(root, out var rootNameSpace);
                if (!string.IsNullOrEmpty(rootNameSpace) && !usings.Contains(rootNameSpace) && (rootNameSpace != nameSpace))
                {
                    code.Insert(nextUsingIndex, $"using {rootNameSpace};");
                }
                if (existMany)
                {
                    code.Add($"{Indent}{Indent}{TodoComment}");
                    code.Add($"{Indent}{Indent}[UserControlDriverIdentify(CustomMethod = \"TryGet\")]");
                    code.Add($"{Indent}{Indent}public static {driverClassName} {funcName}(this {rootDriver} window, int index)");
                    code.Add($"{Indent}{Indent}{Indent}=> new {driverClassName}(new WindowControl(window.Core.GetFromTypeFullName(\"{targetControl.GetType().FullName}\")[index]));");
                    code.Add(string.Empty);
                    code.Add($"{Indent}{Indent}public static void TryGet(this {rootDriver} window, out int[] indices)");
                    code.Add($"{Indent}{Indent}{Indent}=> indices = Enumerable.Range(0, window.Core.GetFromTypeFullName(\"{targetControl.GetType().FullName}\").Length).ToArray();");
                }
                else
                {
                    code.Add($"{Indent}{Indent}[UserControlDriverIdentify]");
                    code.Add($"{Indent}{Indent}public static {driverClassName} {funcName}(this {rootDriver} window)");
                    code.Add($"{Indent}{Indent}{Indent}=> new {driverClassName}(new WindowControl(window.Core.IdentifyFromTypeFullName(\"{targetControl.GetType().FullName}\")));");
                }
                code.Add($"{Indent}}}");
            }
            else if (targetControl is Form)
            {
                code.Add(string.Empty);
                code.Add($"{Indent}public static class {driverClassName}_Extensions");
                code.Add($"{Indent}{{");
                code.Add($"{Indent}{Indent}[WindowDriverIdentify(TypeFullName = \"{targetControl.GetType().FullName}\")]");
                code.Add($"{Indent}{Indent}public static {driverClassName} {GetFuncName(driverClassName)}(this WindowsAppFriend app)");
                code.Add($"{Indent}{Indent}{Indent}=> new {driverClassName}(app.WaitForIdentifyFromTypeFullName(\"{targetControl.GetType().FullName}\"));");
                code.Add($"{Indent}}}");
            }
            code.Add("}");
            return(string.Join(Environment.NewLine, code.ToArray()));
        }
        private DriverInfo <Control> CreateDriverInfo(Control targetControl, string fileName)
        {
            var driverInfo = new DriverInfo <Control>(targetControl);

            var mappedControls = new List <Control>();
            var names          = new List <string>();
            var ancesters      = WindowUtilityInTarget.GetAncesters(targetControl);

            var controlAndDefines = new List <ControlAndDefine>();

            //フィールドから検索
            foreach (var field in GetFields(targetControl))
            {
                //たまにフィールドに親を持っているのがいるのではじく
                if (CollectionUtility.HasReference(ancesters, field.Control))
                {
                    continue;
                }

                //不正なフィールド名のものは取得できない
                if (!_dom.IsValidIdentifier(field.Name))
                {
                    continue;
                }

                //すでにマップされているかチェック
                if (CollectionUtility.HasReference(mappedControls, field.Control))
                {
                    continue;
                }

                //コントロールドライバ
                var driver = DriverCreatorUtils.GetDriverTypeFullName(field.Control, DriverCreatorAdapter.TypeFullNameAndControlDriver);
                if (!string.IsNullOrEmpty(driver))
                {
                    mappedControls.Add(field.Control);
                    var typeName  = DriverCreatorUtils.GetTypeName(driver);
                    var nameSpace = DriverCreatorUtils.GetTypeNamespace(driver);
                    var name      = _customNameGenerator.MakeDriverPropName(field.Control, field.Name, names);
                    var key       = $"Core.Dynamic().{field.Name}";
                    controlAndDefines.Add(new ControlAndDefine(field.Control, $"public {typeName} {name} => new {typeName}({key});"));
                    DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, key, field.Control);
                    if (!driverInfo.Usings.Contains(nameSpace))
                    {
                        driverInfo.Usings.Add(nameSpace);
                    }
                }
                //ユーザーコントロールドライバ
                else if (field.Control is UserControl)
                {
                    mappedControls.Add(field.Control);
                    var name = _customNameGenerator.MakeDriverPropName(field.Control, field.Name, names);
                    names.Add(name);
                    var typeName = _driverTypeNameManager.MakeDriverType(field.Control, out var nameSpace);
                    if (!string.IsNullOrEmpty(nameSpace) && (nameSpace != DriverCreatorAdapter.SelectedNamespace) && !driverInfo.Usings.Contains(nameSpace))
                    {
                        driverInfo.Usings.Add(nameSpace);
                    }
                    var key = $"new WindowControl(Core.Dynamic().{field.Name}";
                    controlAndDefines.Add(new ControlAndDefine(field.Control, $"public {typeName} {name} => new {typeName}({key}));"));
                    DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, key, field.Control);
                }
            }

            //フィールド上に現れないオブジェクトを検索
            CreateDriverInfoFindFromControlTree(-1, targetControl, targetControl, driverInfo, controlAndDefines, mappedControls, names, new int[0], fileName);

            //Sortのロジックがイマイチわかっていない。念のため
            try
            {
                // タブオーダー順のコントロールリスト取得
                var controlList = GetTabOrderChildControls(targetControl);

                // フィールドをタブオーダーでソート
                controlAndDefines.Sort((l, r) => controlList.IndexOf(l.Control) - controlList.IndexOf(r.Control));
            }
            catch { }

            foreach (var e in controlAndDefines)
            {
                driverInfo.Members.Add(e.Define);
            }

            return(driverInfo);
        }