public void TypeNameDictionary()
        {
            TypeNameDictionary <object> l = new TypeNameDictionary <object>();

            l.Add("First", new TestComponentSimple {
                MyProperty = 1
            });
            l.Add("Second", "String!");
            l.Add("Third", long.MaxValue);

            string json = JsonConvert.SerializeObject(l, Formatting.Indented);

            Assert.AreEqual(@"{
  ""First"": {
    ""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
    ""MyProperty"": 1
  },
  ""Second"": ""String!"",
  ""Third"": 9223372036854775807
}", json);

            TypeNameDictionary <object> l2 = JsonConvert.DeserializeObject <TypeNameDictionary <object> >(json);

            Assert.AreEqual(3, l2.Count);

            CustomAssert.IsInstanceOfType(typeof(TestComponentSimple), l2["First"]);
            Assert.AreEqual(1, ((TestComponentSimple)l2["First"]).MyProperty);
            CustomAssert.IsInstanceOfType(typeof(string), l2["Second"]);
            CustomAssert.IsInstanceOfType(typeof(long), l2["Third"]);
        }
Пример #2
0
        private ILType CacheTypeInternal(ILType type)
        {
            var clrType = type as CLRType;

            if (clrType != null)
            {
                //todo 这里还要区分泛型参数是RuntimeType的情况
                TypeToILType[type.TypeForCLR] = type;
            }
            var runtimeType = type as RuntimeType;

            if (runtimeType != null)
            {
                TypeReferenceToTypes[runtimeType.TypeReference.GetHashCode()] = type;
            }
            IdToType[type.Id] = type;
            NameToTypes.Add(type.FullName, type);
            return(type);
        }