/// <summary>
        /// 処理呼び出し。
        /// </summary>
        /// <param name="async">非同期実行用。</param>
        /// <param name="varManager">変数管理。</param>
        /// <param name="typeFinder">タイプ検索。</param>
        /// <param name="info">呼び出し情報。</param>
        /// <returns>戻り値情報。</returns>
        internal static ReturnInfo Execute(IAsyncInvoke async, VarPool varManager, TypeFinder typeFinder, ProtocolInfo info)
        {
            switch (info.ProtocolType)
            {
            case ProtocolType.AsyncResultVarInitialize:
            case ProtocolType.VarInitialize:
                return(VarInitialize(varManager, info));

            case ProtocolType.VarNew:
                return(VarNew(varManager, typeFinder, info));

            case ProtocolType.BinOff:
                return(BinOff(varManager, info));

            case ProtocolType.GetValue:
                return(GetValue(varManager, info));

            case ProtocolType.SetValue:
                return(SetValue(varManager, info));

            case ProtocolType.GetElements:
                return(GetElements(varManager, info));

            case ProtocolType.AsyncOperation:
                return(AsyncOperation(async, varManager, typeFinder, info));

            case ProtocolType.IsEmptyVar:
                return(IsEmptyVar(varManager, info));

            default:
                throw new InternalException();
            }
        }
 public ReturnInfo Execute(IAsyncInvoke async, ProtocolInfo info)
 {
     try
     {
         return(DotNetFriendlyExecutor.Execute(async, _pool, _typeFinder, info));
     }
     catch (Exception e)
     {
         return(new ReturnInfo(new ExceptionInfo(e)));
     }
 }
        static ReturnInfo ExecuteField(IAsyncInvoke async, VarPool varManager, ProtocolInfo info,
                                       object obj, object[] args, FieldInfo field)
        {
            if (args.Length == 0)
            {
                VarAddress getVar = varManager.Add(null);

                KeepAlive(varManager, info.Arguments, getVar);

                async.Execute(delegate
                {
                    ReturnInfo retInfo = new ReturnInfo();
                    try
                    {
                        varManager.SetObject(getVar, field.GetValue(obj));
                    }
                    catch (Exception e)
                    {
                        retInfo = new ReturnInfo(new ExceptionInfo(e));
                    }

                    varManager.SetObject((VarAddress)info.Arguments[0], retInfo);

                    FreeKeepAlive(varManager, info.Arguments, getVar);
                });
                return(new ReturnInfo(getVar));
            }
            else if (args.Length == 1)
            {
                KeepAlive(varManager, info.Arguments, null);

                async.Execute(delegate
                {
                    ReturnInfo retInfo = new ReturnInfo();
                    try
                    {
                        field.SetValue(obj, args[0]);
                    }
                    catch (Exception e)
                    {
                        retInfo = new ReturnInfo(new ExceptionInfo(e));
                    }

                    varManager.SetObject((VarAddress)info.Arguments[0], retInfo);

                    FreeKeepAlive(varManager, info.Arguments, null);
                });
                return(new ReturnInfo());
            }
            throw new InternalException();
        }
        /// <summary>
        /// メソッドorプロパティー実行。
        /// </summary>
        /// <param name="async">非同期実行用。</param>
        /// <param name="varManager">変数管理。</param>
        /// <param name="info">呼び出し情報。</param>
        /// <param name="obj">実行対象オブジェクト。</param>
        /// <param name="args">操作実行引数。</param>
        /// <param name="method">メソッド情報。</param>
        /// <returns>戻り情報。</returns>
        static ReturnInfo ExecuteMethodOrProperty(IAsyncInvoke async, VarPool varManager,
                                                  ProtocolInfo info, object obj, object[] args, MethodInfo method)
        {
            //戻り値
            VarAddress handle = null;

            if (method.ReturnParameter.ParameterType != typeof(void))
            {
                handle = varManager.Add(null);
            }

            //番号管理から消されないようにする
            KeepAlive(varManager, info.Arguments, handle);

            //非同期実行
            async.Execute(delegate
            {
                ReturnInfo retInfo = new ReturnInfo();
                try
                {
                    object retObj = method.Invoke(obj, args);
                    if (method.ReturnParameter.ParameterType != typeof(void))
                    {
                        varManager.SetObject(handle, retObj);
                    }
                    //ref, outの解決
                    List <object> retArgsTmp = new List <object>();
                    retArgsTmp.Add(null); //完了通知変数を戻す。しかし、ここではまだ格納しない
                    retArgsTmp.AddRange(args);
                    ReflectArgsAfterInvoke(varManager, info.Arguments, retArgsTmp.ToArray());
                }
                catch (Exception e)
                {
                    retInfo = new ReturnInfo(new ExceptionInfo(e));
                }

                //完了通知
                varManager.SetObject((VarAddress)info.Arguments[0], retInfo);

                //存命状態を解く
                FreeKeepAlive(varManager, info.Arguments, handle);
            });

            return(new ReturnInfo(handle));
        }
        static ReturnInfo ExecuteMethodOrProperty(IAsyncInvoke async, VarPool varManager,
                                                  ProtocolInfo info, object obj, object[] args, MethodInfo method)
        {
            VarAddress handle = null;

            if (method.ReturnParameter.ParameterType != typeof(void))
            {
                handle = varManager.Add(null);
            }

            KeepAlive(varManager, info.Arguments, handle);

            async.Execute(delegate
            {
                ReturnInfo retInfo = new ReturnInfo();
                try
                {
                    object retObj = method.Invoke(obj, args);
                    if (method.ReturnParameter.ParameterType != typeof(void))
                    {
                        varManager.SetObject(handle, retObj);
                    }
                    List <object> retArgsTmp = new List <object>();
                    retArgsTmp.Add(null);
                    retArgsTmp.AddRange(args);
                    ReflectArgsAfterInvoke(varManager, info.Arguments, retArgsTmp.ToArray());
                }
                catch (Exception e)
                {
                    retInfo = new ReturnInfo(new ExceptionInfo(e));
                }
                varManager.SetObject((VarAddress)info.Arguments[0], retInfo);

                FreeKeepAlive(varManager, info.Arguments, handle);
            });

            return(new ReturnInfo(handle));
        }
        /// <summary>
        /// 実行名称と引数によって実施するオペレーションを判断する。
        /// 非同期実行部分以外のコードはスレッドセーフでなければならない。
        /// </summary>
        /// <param name="async">非同期実行用。</param>
        /// <param name="varManager">変数管理。</param>
        /// <param name="typeFinder">タイプ検索。</param>
        /// <param name="info">呼び出し情報。</param>
        /// <returns>戻り値情報。</returns>
        static ReturnInfo AsyncOperation(IAsyncInvoke async, VarPool varManager, TypeFinder typeFinder, ProtocolInfo info)
        {
            Type   type;
            object obj;

            object[]     args;
            Type[]       argTypesOri;
            BindingFlags bind;

            //実行対象の解決
            ResolveInvokeTarget(varManager, typeFinder, info, out type, out obj, out args, out argTypesOri, out bind);

            //第一引数は完了時結果格納変数
            List <object> argTmp = new List <object>(args);

            argTmp.RemoveAt(0);
            args = argTmp.ToArray();
            List <Type> argTypeTmp = new List <Type>(argTypesOri);

            argTypeTmp.RemoveAt(0);
            argTypesOri = argTypeTmp.ToArray();

            //操作解決用型情報取得
            if (info.OperationTypeInfo != null)
            {
                type = typeFinder.GetType(info.OperationTypeInfo.Target);
                if (type == null)
                {
                    throw new InformationException(string.Format(CultureInfo.CurrentCulture,
                                                                 ResourcesLocal.Instance.UnknownTypeInfoFormat, info.OperationTypeInfo.Target));
                }
            }

            //操作名称最適化
            string operation     = OptimizeOperationName(type, info.Operation, args.Length);
            Type   findStartType = type;

            //引数の型を取得
            Type[] argTypes = GetArgTypes(typeFinder, info.OperationTypeInfo, argTypesOri);

            //操作検索
            bool isObjectArrayArg = false;
            int  nameMatchCount   = 0;
            bool first            = true;
            bool isAmbiguousArgs  = false;

            while (!isAmbiguousArgs && type != null)
            {
                //親の型へ。
                if (!first)
                {
                    type = type.BaseType;
                    if (type == null)
                    {
                        break;
                    }
                }
                first = false;

                //フィールド
                FieldInfo field = FindField(type, bind, operation, args, ref isObjectArrayArg, ref nameMatchCount);
                if (field != null)
                {
                    return(ExecuteField(async, varManager, info, obj, args, field));
                }

                //プロパティーとメソッド
                MethodInfo method = FindMethodOrProperty(info.OperationTypeInfo != null, type, bind, operation, argTypes,
                                                         ref isObjectArrayArg, ref nameMatchCount, ref isAmbiguousArgs);
                if (method != null)
                {
                    return(ExecuteMethodOrProperty(async, varManager, info, obj, args, method));
                }
            }

            //結局発見できなかった。
            throw MakeNotFoundException(info, findStartType, argTypes, isObjectArrayArg, nameMatchCount, isAmbiguousArgs);
        }
        /// <summary>
        /// create object.
        /// </summary>
        static ReturnInfo VarNew(IAsyncInvoke async, VarPool varManager, TypeFinder typeFinder, ProtocolInfo info)
        {
            Type type = typeFinder.GetType(info.TypeFullName);

            if (type == null)
            {
                throw new InformationException(string.Format(CultureInfo.CurrentCulture, ResourcesLocal.Instance.UnknownTypeInfoFormat, info.TypeFullName));
            }

            object[] args;
            Type[]   argTypesOri;
            ResolveArgs(varManager, info.Arguments, out args, out argTypesOri);
            Type[] argTypes = GetArgTypes(typeFinder, info.OperationTypeInfo, argTypesOri);

            //value type.
            if (argTypes.Length == 0 && type.IsValueType())
            {
                return(new ReturnInfo(varManager.Add(Activator.CreateInstance(type))));
            }

            //resolve overload.
            ConstructorInfo[]      constructorInfos = type.GetConstructors();
            List <ConstructorInfo> constructorList  = new List <ConstructorInfo>();
            bool isObjectArrayArg = false;

            for (int i = 0; i < constructorInfos.Length; i++)
            {
                ParameterInfo[] paramInfos = constructorInfos[i].GetParameters();
                bool            isPerfect;
                bool            isObjectArrayArgTmp;
                if (IsMatchParameter(info.OperationTypeInfo != null, argTypes, paramInfos, out isPerfect, out isObjectArrayArgTmp))
                {
                    if (isPerfect)
                    {
                        constructorList.Clear();
                        constructorList.Add(constructorInfos[i]);
                        break;
                    }
                    constructorList.Add(constructorInfos[i]);
                }
                if (isObjectArrayArgTmp)
                {
                    isObjectArrayArg = true;
                }
            }

            //not found.
            if (constructorList.Count == 0)
            {
                if (isObjectArrayArg)
                {
                    throw new InformationException(string.Format(CultureInfo.CurrentCulture, ResourcesLocal.Instance.ErrorNotFoundConstractorFormatForObjectArray,
                                                                 type.Name, MakeErrorInvokeArgInfo(argTypes)));
                }
                else
                {
                    throw new InformationException(string.Format(CultureInfo.CurrentCulture, ResourcesLocal.Instance.ErrorNotFoundConstractorFormat,
                                                                 type.Name, MakeErrorInvokeArgInfo(argTypes)));
                }
            }
            if (constructorList.Count != 1)
            {
                //can't resolve overload.
                throw new InformationException(string.Format(CultureInfo.CurrentCulture, ResourcesLocal.Instance.ErrorManyFoundConstractorFormat,
                                                             type.Name, MakeErrorInvokeArgInfo(argTypes)));
            }

            //create.
            bool   isCreated = false;//TODO
            object instance  = null;

            async.Execute(() =>
            {
                instance  = constructorList[0].Invoke(args);
                isCreated = true;
            });
            while (!isCreated)
            {
                Thread.Sleep(0);
            }

            //resolve ref, out.
            ReflectArgsAfterInvoke(varManager, info.Arguments, args);

            //entry object.
            return(new ReturnInfo(varManager.Add(instance)));
        }