예제 #1
0
 public CSharpToJavascriptConverterTests()
 {
     _Cacher = Substitute.For <IJavascriptSessionCache>();
     _Cacher.When(c => c.CacheFromCSharpValue(Arg.Any <object>(), Arg.Any <IJSCSGlue>()))
     .Do(callInfo => _Cache.Add(callInfo[0], (IJSCSGlue)callInfo[1]));
     _Cacher.GetCached(Arg.Any <object>()).Returns(callInfo => _Cache.GetOrDefault(callInfo[0]));
     _GlueFactory       = new GlueFactory(null, null);
     _Logger            = Substitute.For <IWebSessionLogger>();
     _IWebBrowserWindow = Substitute.For <IWebBrowserWindow>();
     _IWebBrowserWindow.IsTypeBasic(typeof(string)).Returns(true);
     _CSharpToJavascriptConverter = new CSharpToJavascriptConverter(_IWebBrowserWindow, _Cacher, _GlueFactory, _Logger);
 }
예제 #2
0
        public IJSCSGlue Map(object from, object additional = null)
        {
            if (from == null)
            {
                return(new JSBasicObject(null));
            }

            var res = _Cacher.GetCached(from);

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

            var command = from as ICommand;

            if (command != null)
            {
                return(_CommandFactory.Build(command));
            }

            var simpleCommand = from as ISimpleCommand;

            if (simpleCommand != null)
            {
                return(_CommandFactory.Build(simpleCommand));
            }

            var resultCommand = from as IResultCommand;

            if (resultCommand != null)
            {
                return(_CommandFactory.Build(resultCommand));
            }

            var type = from.GetType();

            if (_Context.IsTypeBasic(type))
            {
                return(new JSBasicObject(from));
            }

            if (type.IsEnum)
            {
                var trueres = new JSBasicObject(from);
                _Cacher.Cache(from, trueres);
                return(trueres);
            }

            var ienfro = from as IEnumerable;

            if (ienfro != null)
            {
                return(Convert(ienfro));
            }

            var propertyInfos = GetPropertyInfos(from).Concat(GetPropertyInfos(additional)).ToList();

            var gres = new JsGenericObject(from, propertyInfos.Count);

            _Cacher.Cache(from, gres);

            MappNested(gres, propertyInfos);
            return(gres);
        }
        public IJSCSGlue Map(object from, object additional = null)
        {
            if (from == null)
            {
                return(_Null ?? (_Null = new JSBasicObject(null)));
            }

            var res = _Cacher.GetCached(from);

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

            var type = from.GetType();

            if (_Context.IsTypeBasic(type))
            {
                res = new JSBasicObject(from);
                _Cacher.CacheFromCSharpValue(from, res);
                return(res);
            }

            var command = from as ICommand;

            if (command != null)
            {
                return(_GlueFactory.Build(command));
            }

            var simpleCommand = from as ISimpleCommand;

            if (simpleCommand != null)
            {
                return(_GlueFactory.Build(simpleCommand));
            }

            var resultCommand = from as IResultCommand;

            if (resultCommand != null)
            {
                return(_GlueFactory.Build(resultCommand));
            }

            if (type.IsEnum)
            {
                var trueres = new JSBasicObject(from);
                _Cacher.CacheFromCSharpValue(from, trueres);
                return(trueres);
            }

            var ienfro = from as IEnumerable;

            if (ienfro != null)
            {
                return(Convert(ienfro));
            }

            var propertyInfos           = @from.GetType().GetReadProperties();
            var additionalPropertyInfos = additional?.GetType().GetReadProperties();

            var gres = _GlueFactory.Build(from, propertyInfos.Count + (additionalPropertyInfos?.Count ?? 0));

            _Cacher.CacheFromCSharpValue(from, gres);

            MappNested(gres, @from, propertyInfos);
            MappNested(gres, additional, additionalPropertyInfos);
            return(gres);
        }