Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IEnumerableConverterFactory.cs
Go to the documentation of this file.
7
9
11{
13
15
17
18 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
20 {
21 }
22
23 public override bool CanConvert(Type typeToConvert)
24 {
25 return typeof(IEnumerable).IsAssignableFrom(typeToConvert);
26 }
27
28 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The ctor is marked RequiresUnreferencedCode.")]
30 {
31 Type type = null;
32 Type type2 = null;
35 if (typeToConvert.IsArray)
36 {
37 if (typeToConvert.GetArrayRank() > 1)
38 {
40 }
42 type = typeToConvert.GetElementType();
43 }
44 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericBaseClass(typeof(List<>))) != null)
45 {
47 type = compatibleGenericBaseClass.GetGenericArguments()[0];
48 }
49 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericBaseClass(typeof(Dictionary<, >))) != null)
50 {
51 Type[] genericArguments = compatibleGenericBaseClass.GetGenericArguments();
55 }
56 else if (typeToConvert.IsImmutableDictionaryType())
57 {
58 Type[] genericArguments = typeToConvert.GetGenericArguments();
62 }
63 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericInterface(typeof(IDictionary<, >))) != null)
64 {
65 Type[] genericArguments = compatibleGenericBaseClass.GetGenericArguments();
69 }
70 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericInterface(typeof(IReadOnlyDictionary<, >))) != null)
71 {
72 Type[] genericArguments = compatibleGenericBaseClass.GetGenericArguments();
76 }
77 else if (typeToConvert.IsImmutableEnumerableType())
78 {
80 type = typeToConvert.GetGenericArguments()[0];
81 }
82 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericInterface(typeof(IList<>))) != null)
83 {
85 type = compatibleGenericBaseClass.GetGenericArguments()[0];
86 }
87 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericInterface(typeof(ISet<>))) != null)
88 {
90 type = compatibleGenericBaseClass.GetGenericArguments()[0];
91 }
92 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericInterface(typeof(ICollection<>))) != null)
93 {
95 type = compatibleGenericBaseClass.GetGenericArguments()[0];
96 }
97 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericBaseClass(typeof(Stack<>))) != null)
98 {
100 type = compatibleGenericBaseClass.GetGenericArguments()[0];
101 }
102 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericBaseClass(typeof(Queue<>))) != null)
103 {
105 type = compatibleGenericBaseClass.GetGenericArguments()[0];
106 }
107 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericBaseClass(typeof(ConcurrentStack<>))) != null)
108 {
110 type = compatibleGenericBaseClass.GetGenericArguments()[0];
111 }
112 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericBaseClass(typeof(ConcurrentQueue<>))) != null)
113 {
115 type = compatibleGenericBaseClass.GetGenericArguments()[0];
116 }
117 else if ((compatibleGenericBaseClass = typeToConvert.GetCompatibleGenericInterface(typeof(IEnumerable<>))) != null)
118 {
120 type = compatibleGenericBaseClass.GetGenericArguments()[0];
121 }
122 else if (typeof(IDictionary).IsAssignableFrom(typeToConvert))
123 {
125 {
127 }
129 }
130 else if (typeof(IList).IsAssignableFrom(typeToConvert))
131 {
132 if (typeToConvert == typeof(IList))
133 {
134 return s_converterForIList;
135 }
137 }
138 else if (typeToConvert.IsNonGenericStackOrQueue())
139 {
141 }
142 else
143 {
145 {
147 }
149 }
150 return (JsonConverter)Activator.CreateInstance(typeFromHandle.GetGenericArguments().Length switch
151 {
152 1 => typeFromHandle.MakeGenericType(typeToConvert),
153 2 => typeFromHandle.MakeGenericType(typeToConvert, type),
154 _ => typeFromHandle.MakeGenericType(typeToConvert, type2, type),
155 }, BindingFlags.Instance | BindingFlags.Public, null, null, null);
156 }
157}
static ? object CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture)
Definition Activator.cs:17
static readonly IEnumerableConverter< IEnumerable > s_converterForIEnumerable
override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
static readonly IDictionaryConverter< IDictionary > s_converterForIDictionary
static void ThrowNotSupportedException_SerializationNotSupported(Type propertyType)