public static void IteratorNext(BinaryReader br, BinaryWriter bw)
        {
            if (!GetAW(br, bw, out var autoWrap))
            {
                return;
            }

            try
            {
                var enumerator = (System.Collections.IEnumerator)autoWrap.Object;
                var res        = enumerator.MoveNext();
                bw.Write(true);
                if (!res)
                {
                    AutoWrap.ObjectsList.RemoveKey(autoWrap.IndexInStorage);
                    bw.Write(false);
                    return;
                }

                bw.Write(true);
                WorkWithVariant.WriteObject(AutoWrap.WrapObject(enumerator.Current), bw);
            }
            catch (Exception e)
            {
                SetError(AutoWrap.GetExceptionString("Ошибка итератора", "", e), bw);
            }
        }
        public static bool CallAsFuncAll(BinaryReader br, BinaryWriter bw, out object result, bool writeResult)
        {
            result = null;
            if (!GetAW(br, bw, out var autoWrap))
            {
                return(false);
            }

            string methodName = br.ReadString();
            var    args       = GetArrayParams(br);

            List <int> changeParameters = new List <int>();

            var res = autoWrap.TryInvokeMember(methodName, args, out result, changeParameters, out var error);

            if (!res)
            {
                SetError(error, bw);
                return(false);
            }

            if (writeResult)
            {
                bw.Write(true);
                WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
                WriteChangeParams(bw, args, changeParameters);
            }
            return(true);
        }
        public static void GetIndex(BinaryReader br, BinaryWriter bw)
        {
            if (!GetAW(br, bw, out var autoWrap))
            {
                return;
            }

            var    parameters = GetArrayParams(br);
            string methodName = "get_Item";

            if (typeof(Array).IsAssignableFrom(autoWrap.Type))
            {
                methodName = "GetValue";
            }

            var changeParams = new List <int>();
            var res          = autoWrap.TryInvokeMember(methodName, parameters, out var result, changeParams, out var error);

            if (!res)
            {
                SetError(error, bw);
            }
            else
            {
                bw.Write(true);
                WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
            }
        }
        public static void CallAsDelegate(BinaryReader br, BinaryWriter bw)
        {
            object result;

            if (!GetAW(br, bw, out var autoWrap))
            {
                return;
            }

            var args = GetArrayParams(br);

            try
            {
                var del = (Delegate)autoWrap.Object;
                result = del.DynamicInvoke(args);
            }
            catch (Exception e)
            {
                SetError(AutoWrap.GetExceptionString($"Ошибка вызова делегата Target = ", "", e), bw);
                return;
            }

            bw.Write(true);
            WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
        }
        private static void WriteChangeParams(BinaryWriter bw, object[] args, List <int> changeParameters)
        {
            bw.Write(changeParameters.Count);

            foreach (var i in changeParameters)
            {
                bw.Write(i);
                WorkWithVariant.WriteObject(AutoWrap.WrapObject(args[i]), bw);
            }
        }
        internal void SendEvent(Guid eventKey, object result)
        {
            MemoryStream stream = new MemoryStream();
            var          bw     = new BinaryWriter(stream);

            bw.Write((byte)1);
            bw.Write(eventKey.ToByteArray());
            WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
            bw.Flush();

            SendStream(stream);
        }
        internal void SendAsyncMessage(bool successfully, object result)
        {
            MemoryStream stream = new MemoryStream();
            var          bw     = new BinaryWriter(stream);

            bw.Write((byte)0);
            bw.Write(_key.ToByteArray());
            bw.Write(successfully);
            WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
            bw.Flush();

            SendStream(stream);
        }
        //public static void GetWrapperForObjectWithEvents(BinaryReader br, BinaryWriter bw, IPAddress address)
        //{
        //    try
        //    {
        //        if (!GetAW(br, bw, out var autoWrap)) return;

        //        Type type = autoWrap.Type;
        //        Type genType = typeof(WrapperForEvents<>);
        //        Type constructed = genType.MakeGenericType(type);
        //        var propertyName = "WrapperCreator";

        //        var fi = constructed.GetField(propertyName);
        //        Delegate func = (Delegate)fi.GetValue(null);

        //        //var mi = constructed.GetMethod("СоздатьОбертку");
        //        // Delegate функция = (Delegate)mi.Invoke(null,null);

        //        var taskId = new Guid();
        //        var port = br.ReadInt32();

        //        var ac = new TcpAsyncCallBack(taskId, address, port);
        //        var callBack = new Action<Guid, object>(ac.SendEvent);

        //        bw.Write(true);
        //        WorkWithVariant.WriteObject(AutoWrap.WrapObject(func.DynamicInvoke(callBack, autoWrap.Object)), bw);
        //    }
        //    catch (Exception e)
        //    {
        //        SetError(AutoWrap.GetExceptionString("Ошибка создания оберки событий", "", e), bw);
        //    }
        //}

        public static void GetPropVal(BinaryReader br, BinaryWriter bw)
        {
            if (!GetAW(br, bw, out var autoWrap))
            {
                return;
            }

            string propertyName = br.ReadString();
            var    res          = autoWrap.TryGetMember(propertyName, out var result, out var error);

            if (!res)
            {
                SetError(error, bw);
                return;
            }

            bw.Write(true);
            WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
        }
        private static void CallStaticMethod(BinaryWriter bw, Type T, string methodName, object[] args)
        {
            try
            {
                var method = InformationOnTheTypes.FindMethod(T, true, methodName, args);

                if (method == null)
                {
                    SetError($"Нет найден метод  {method} для типа {T}", bw);
                    return;
                }

                var obj = method.ExecuteMethod(null, args);

                bw.Write(true);
                WorkWithVariant.WriteObject(AutoWrap.WrapObject(obj), bw);
                bw.Write((int)0);
            }
            catch (Exception e)
            {
                SetError(AutoWrap.GetExceptionString("Ошибка бинарной операции ", "", e), bw);
            }
        }
Beispiel #10
0
        public static bool CallAsGenericFuncAll(BinaryReader br, BinaryWriter bw, out object result, bool writeResult)
        {
            result = null;;
            if (!GetAW(br, bw, out var autoWrap))
            {
                return(false);
            }

            string methodName = br.ReadString();
            var    arguments  = GetArrayParams(br);
            var    Params     = GetArrayParams(br);

            // Можно параметры передавать ввиде типов и строк
            var genericArguments = new Type[arguments.Length];

            for (int i = 0; i < genericArguments.Length; i++)
            {
                genericArguments[i] = NetObjectToNative.FindTypeForCreateObject(arguments[i]);
            }

            result = null;
            var typesOfParameters = AllMethodsForName.GetTypesParameters(Params);
            var res = InformationOnTheTypes.FindGenericMethodsWithGenericArguments(
                autoWrap.Type,
                autoWrap.IsType,
                methodName,
                genericArguments,
                typesOfParameters);

            if (res == null)
            {
                SetError("Не найден дженерик метод " + methodName, bw);
                return(false);
            }

            try
            {
                var copyParams = new object[Params.Length];
                Params.CopyTo(copyParams, 0);

                var obj = autoWrap.IsType ? null : autoWrap.Object;
                result = res.ExecuteMethod(obj, copyParams);

                var returnType = res.Method.ReturnType;

                if (result != null && returnType.GetTypeInfo().IsInterface)
                {
                    result = new AutoWrap(result, returnType);
                }

                if (writeResult)
                {
                    bw.Write(true);
                    WorkWithVariant.WriteObject(AutoWrap.WrapObject(result), bw);
                    WriteChangeParams(bw, copyParams, AutoWrap.GetChangeParams(Params, copyParams));
                }
            }
            catch (Exception e)
            {
                SetError(AutoWrap.GetExceptionString($"Ошибка вызова Дженерик метода {methodName}", "", e), bw);
                return(false);
            }

            return(true);
        }