コード例 #1
0
        private static GetString TryToCustomObjectLiteralToStringMethod(object o)
        {
            if (o == null)
            {
                return(null);
            }

            if (!ObjectLiteralDetection.IsObjectLiteral(o, out var type, out var uniqueId))
            {
                return(null);
            }

            if (_cache.Contains(uniqueId))
            {
                return(_cache.Get(uniqueId));
            }

            GetString customMethodToCallIfAny;
            var       toStringMethodInfo = type.GetMethod(nameof(ToString), BindingFlags.Public | BindingFlags.Instance, new Type[0]);

            if (toStringMethodInfo == null)
            {
                customMethodToCallIfAny = null;
            }
            else
            {
                var javaScriptToStringMethodName = Script.Write <string>("{0}.sn", toStringMethodInfo);
                if (!Script.Write <bool>("{0}", javaScriptToStringMethodName))
                {
                    customMethodToCallIfAny = null;
                }
                else
                {
                    customMethodToCallIfAny = Script.Write <GetString>("{0}.prototype[{1}]", type, javaScriptToStringMethodName);
                }
            }
            _cache.Set(uniqueId, customMethodToCallIfAny);
            return(customMethodToCallIfAny);
        }
コード例 #2
0
        private static AreEqual TryToCustomObjectLiteralEqualsMethod(object o)
        {
            if (o == null)
            {
                return(null);
            }

            if (!ObjectLiteralDetection.IsObjectLiteral(o, out var type, out var uniqueId))
            {
                return(null);
            }

            if (_cache.Contains(uniqueId))
            {
                return(_cache.Get(uniqueId));
            }

            AreEqual customMethodToCallIfAny;
            var      equalsMethodInfo = type.GetMethod(nameof(Equals), BindingFlags.Public | BindingFlags.Instance, new[] { typeof(object) });

            if (equalsMethodInfo == null)
            {
                customMethodToCallIfAny = null;
            }
            else
            {
                var javaScriptEqualsMethodName = Script.Write <string>("{0}.sn", equalsMethodInfo);
                if (!Script.Write <bool>("{0}", javaScriptEqualsMethodName))
                {
                    customMethodToCallIfAny = null;
                }
                else
                {
                    customMethodToCallIfAny = Script.Write <AreEqual>("{0}.prototype[{1}]", type, javaScriptEqualsMethodName);
                }
            }
            _cache.Set(uniqueId, customMethodToCallIfAny);
            return(customMethodToCallIfAny);
        }