Terraria ModLoader  0.11.4
A framework for Terraria mods
Terraria.ModLoader.Config.ToFromStringConverter< T > Class Template Reference

This TypeConverter facilitates converting to and from the string Type. This is necessary for Objects that are to be used as Dictionary keys, since the JSON for keys needs to be a string. Classes annotated with this TypeConverter need to implement a static FromString method that returns T. More...

+ Inheritance diagram for Terraria.ModLoader.Config.ToFromStringConverter< T >:
+ Collaboration diagram for Terraria.ModLoader.Config.ToFromStringConverter< T >:

Public Member Functions

override bool CanConvertFrom (ITypeDescriptorContext context, System.Type sourceType)
 
override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
 
override object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
 

Detailed Description

This TypeConverter facilitates converting to and from the string Type. This is necessary for Objects that are to be used as Dictionary keys, since the JSON for keys needs to be a string. Classes annotated with this TypeConverter need to implement a static FromString method that returns T.

Template Parameters
TThe Type that implementes the static FromString method that returns Type T.

Definition at line 145 of file EntityDefinition.cs.

Member Function Documentation

override bool Terraria.ModLoader.Config.ToFromStringConverter< T >.CanConvertFrom ( ITypeDescriptorContext  context,
System.Type  sourceType 
)

Definition at line 151 of file EntityDefinition.cs.

151  {
152  if (sourceType == typeof(string)) {
153  return true;
154  }
155  return base.CanConvertFrom(context, sourceType);
156  }
override bool Terraria.ModLoader.Config.ToFromStringConverter< T >.CanConvertTo ( ITypeDescriptorContext  context,
Type  destinationType 
)

Definition at line 147 of file EntityDefinition.cs.

147  {
148  return destinationType != typeof(string); // critical for populating from json string. Does prevent compact json values though.
149  }
override object Terraria.ModLoader.Config.ToFromStringConverter< T >.ConvertFrom ( ITypeDescriptorContext  context,
System.Globalization.CultureInfo  culture,
object  value 
)

Definition at line 157 of file EntityDefinition.cs.

157  {
158  if (value is string) {
159  MethodInfo parse = typeof(T).GetMethod("FromString", new Type[] { typeof(string) });
160  if (parse != null && parse.IsStatic && parse.ReturnType == typeof(T)) {
161  return parse.Invoke(null, new object[] { value });
162  }
163 
164  throw new JsonException(string.Format(
165  "The {0} type does not have a public static FromString(string) method that returns a {0}.",
166  typeof(T).Name));
167  }
168  return base.ConvertFrom(context, culture, value);
169  }