예제 #1
0
        public static bool ExecuteGenericMethod(object obj, Type type, string methodName, bool isStatic, Type[] arguments, Type[] variableType, object[] parameters, out object result)
        {
            result = null;

            var res = InformationOnTheTypes.FindGenericMethodsWithGenericArguments(type, isStatic, methodName, arguments, variableType);

            if (res == null)
            {
                return(false);
            }

            try
            {
                result = res.Invoke(obj, parameters);

                if (result != null && res.ReturnType.GetTypeInfo().IsInterface)
                {
                    result = new AutoWrap(result, res.ReturnType);
                }
            }
            catch (Exception)
            {
                return(false);
            }
            return(true);
        }
예제 #2
0
        public static bool ExecuteExtensionMethod(AutoWrap autoWrap, string methodName, object[] parameters, out object result)
        {
            result = null;
            if (autoWrap.IsType)
            {
                if (methodName == "GetTypeInfo")
                {
                    result = autoWrap.Type.GetTypeInfo();
                    return(true);
                }

                return(false);
            }

            var assembly = autoWrap.Type.GetTypeInfo().Assembly;
            var types    = GetExtensionMethods(assembly, autoWrap.Type, methodName);

            if (CheckExtensionMethods(types, autoWrap, methodName, parameters, out result))
            {
                return(true);
            }

            if (autoWrap.Type.IsGenericTypeOf(_enumerableType.GetTypeInfo(), _enumerableType))
            {
                types = FindType(_linqExtensionTypes, autoWrap.Type, methodName, (method) => method.IsGenericMethod);
                return(CheckExtensionMethods(types, autoWrap, methodName, parameters, out result));
            }

            return(false);
        }
예제 #3
0
        public object InvokeWithDefaultParameters(object target, object[] input, int parametersCount)
        {
            if (input.Length == parametersCount)
            {
                return(Invoke(target, input));
            }
            object[]        parametersValue = new object[parametersCount];
            ParameterInfo[] parameters      = Method.GetParameters();
            Array.Copy(input, parametersValue, input.Length);

            for (int i = input.Length; i < parameters.Length; i++)
            {
                parametersValue[i] = parameters[i].RawDefaultValue;
            }

            var res = Invoke(target, parametersValue);

            Array.Copy(parametersValue, input, input.Length);

            if (res != null && ReturnType != null)
            {
                res = new AutoWrap(res, ReturnType);
            }
            return(res);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        public int Add(AutoWrap obj)
        {
            var element = new StorageElem(obj);
            var gotLock = false;

            try
            {
                _lockObject.Enter(ref gotLock);

                if (FirstDeleted == -1)
                {
                    return(AddInArray(element));
                }
                else
                {
                    int newPos = FirstDeleted;
                    FirstDeleted      = _elements[newPos].Next;
                    _elements[newPos] = element;
                    return(newPos);
                }
            }
            finally
            {
                if (gotLock)
                {
                    _lockObject.Exit();
                }
            }
        }
예제 #7
0
        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);
            }
        }
예제 #8
0
        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);
            }
        }
예제 #9
0
        private static bool CheckExtensionMethods(Type[] types, AutoWrap autoWrap, string methodName, object[] parameters,
                                                  out object result)
        {
            result = null;
            if (types.Length == 0)
            {
                return(false);
            }

            var args = new object[parameters.Length + 1];

            Array.Copy(parameters, 0, args, 1, parameters.Length);
            args[0] = autoWrap.Object;
            foreach (var type in types)
            {
                try
                {
                    var method = InformationOnTheTypes.FindMethod(type, true, methodName, args);
                    if (method != null)
                    {
                        result = method.ExecuteMethod(null, args);
                        Array.Copy(args, 1, parameters, 0, parameters.Length);

                        return(true);
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            return(false);
        }
예제 #10
0
        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);
            }
        }
        object IPropertyOrFieldInfo.GetValue(object obj)
        {
            var res = PropertyInfo.GetValue(obj);

            if (res != null && ReturnType != null)
            {
                res = new AutoWrap(res, ReturnType);
            }
            return(res);
        }
예제 #12
0
        public GenericExecutor(AutoWrap wrap, object[] arguments)
        {
            _arguments = new Type[arguments.Length];

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

            _wrap = wrap;
        }
예제 #13
0
        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);
        }
예제 #14
0
        // Обернем объекты для посылки на сервер
        // Напрямую передаются только числа, строки, DateTime, Guid, byte[]
        public static object WrapObject(object obj)
        {
            if (obj == null)
            {
                return(null);
            }
            if (!WorkWithVariant.MatchTypes.ContainsKey(obj.GetType()))
            {
                obj = new AutoWrap(obj);
            }

            return(obj);
        }
예제 #15
0
        public IEnumerator GetEnumerator()
        {
            foreach (var str in _enumerator)
            {
                object res = null;
                if (str != null && _typeInfo.IsInstanceOfType(str))
                {
                    res = new AutoWrap(str, _type);
                }

                yield return(res);
            }
        }
예제 #16
0
        private static bool GetAW(BinaryReader br, BinaryWriter bw, out AutoWrap autoWrap)
        {
            var target = br.ReadInt32();

            autoWrap = AutoWrap.ObjectsList.GetValue(target);

            if (autoWrap == null)
            {
                SetError("Не найдена ссылка на объект", bw);
                return(false);
            }

            return(true);
        }
예제 #17
0
 public static void DeleteObjects(BinaryReader br, BinaryWriter bw)
 {
     try
     {
         int count = br.ReadInt32();
         for (int i = 0; i < count; i++)
         {
             int target = br.ReadInt32();
             AutoWrap.ObjectsList.RemoveKey(target);
         }
         bw.Write(true);
         WorkWithVariant.WriteObject(null, bw);
     }
     catch (Exception e)
     {
         SetError(AutoWrap.GetExceptionString("Ошибка удаления объектов", "", e), bw);
     }
 }
예제 #18
0
        public object ExecuteMethod(object Target, object[] input)
        {
            if (!(HasParams || HasDefaultValue))
            {
                return(Invoke(Target, input));
            }

            int parametersCount = (ParamsCount > 0) ? ParamsCount : ParametersCount;

            if (HasDefaultValue)
            {
                return(InvokeWithDefaultParameters(Target, input, parametersCount));
            }

            int lastParameterIndex = parametersCount - 1;

            object[] realParams = new object[parametersCount];
            for (int i = 0; i < lastParameterIndex; i++)
            {
                realParams[i] = input[i];
            }

            Array parameters = Array.CreateInstance(TypeParams, input.Length - lastParameterIndex);

            for (int i = 0; i < parameters.Length; i++)
            {
                parameters.SetValue(input[i + lastParameterIndex], i);
            }

            realParams[lastParameterIndex] = parameters;

            var res = Invoke(Target, realParams);

            parameters = (Array)realParams[lastParameterIndex];
            for (int i = 0; i < parameters.Length; i++)
            {
                input[i + lastParameterIndex] = parameters.GetValue(i);
            }
            if (res != null && ReturnType != null)
            {
                res = new AutoWrap(res, ReturnType);
            }
            return(res);
        }
예제 #19
0
        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);
        }
예제 #20
0
        public static void CallAsyncFunc(BinaryReader br, BinaryWriter bw, IPAddress address)
        {
            if (!CallAsFuncAll(br, bw, out var result, false))
            {
                return;
            }
            try
            {
                var taskId   = new Guid(br.ReadBytes(16));
                var port     = br.ReadInt32();
                var typeInfo = result.GetType().GetTypeInfo();

                var asyncCallBack = new TcpAsyncCallBack(taskId, address, port);
                var callBack      = new Action <bool, object>(asyncCallBack.SendAsyncMessage);
                if (!typeInfo.IsGenericType)
                {
                    AsyncRunner.TaskExecute((Task)result, callBack);
                    return;
                }

                var args   = new[] { result, callBack };
                var method = InformationOnTheTypes.FindMethod(typeof(AsyncRunner), true, "Execute", args);

                if (method == null)
                {
                    SetError("Неверный результат", bw);
                    return;
                }

                method.ExecuteMethod(null, args);
            }
            catch (Exception e)
            {
                SetError(AutoWrap.GetExceptionString("Ошибка вызова делегата", "", e), bw);
                return;
            }

            bw.Write(true);
            WorkWithVariant.WriteObject(null, bw);
        }
예제 #21
0
        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);
            }
        }
 public static Object ToInt(object valueOrig) => new AutoWrap(Convert.ChangeType(
                                                                  AutoWrap.GetRalObject(valueOrig),
                                                                  typeof(Int32),
                                                                  CultureInfo.InvariantCulture));
 public static object CreateObject(object type) =>
 AutoWrap.WrapObject(System.Activator.CreateInstance(FindTypeForCreateObject(type)));
예제 #24
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);
        }
예제 #25
0
 internal StorageElem(AutoWrap wrap)
 {
     Wrap = wrap;
     Next = -1;
 }
예제 #26
0
 internal StorageElem(AutoWrap wrap, int next)
 {
     Wrap = wrap;
     Next = next;
 }
 // Нужен для получения типа неподдерживаемого 1С, беззнаковые,Decimal итд
 public static Object ChangeType(object type, object valueOrig) =>
 new AutoWrap(
     Convert.ChangeType(AutoWrap.GetRalObject(valueOrig),
                        FindTypeForCreateObject(type),
                        CultureInfo.InvariantCulture));