public IFormatter Create(Type type) { Type underlyingType = type.GetEnumUnderlyingType(); IFormatter underlyingFormatter = FormatterManager.GetFormatterAsync(underlyingType, false).Result; Context context = new Context(type, underlyingFormatter, underlyingType); return(context.BuildFormatter()); }
private void CreateItemFormatter() { Task <IFormatter> itemFormatter = FormatterManager.GetFormatterAsync(ItemType, isPolymorph: true); Type formatterType = typeof(Task <IFormatter>); string fieldName = "ItemFormatter"; TypeBuilder helperBuilder = GeneratedAssembly.CreateType(Type.Name + "FormatterHelper"); FieldBuilder cachedFormatterField = helperBuilder.DefineField(fieldName, formatterType, FieldAttributes.Static | FieldAttributes.Public); Type helperType = helperBuilder.CreateType(); FieldInfo realFieldInfo = helperType.GetField(fieldName); realFieldInfo.SetValue(null, itemFormatter); _itemFormatterField = realFieldInfo; _itemFormatterType = typeof(Formatter <>).MakeGenericType(ItemType); }
private void CreateSubFormatterFields() { _subFormatters = new Dictionary <FieldBase, FieldInfo>(); TypeBuilder helperBuilder = GeneratedAssembly.CreateType(_type.Name + "FormatterHelper"); Dictionary <string, Task <IFormatter> > values = new Dictionary <string, Task <IFormatter> >(); Dictionary <string, FieldBase> fieldMap = new Dictionary <string, FieldBase>(); foreach (FieldBase field in _fields) { Task <IFormatter> formatterTask = FormatterManager.GetFormatterAsync(field.Type, field.Type.IsAbstract); Type fieldType = typeof(Task <IFormatter>); string fieldName = String.Format("_0x{0}_formatter", field.ID.ToString("x2")); FieldBuilder cachedFormatterField = helperBuilder.DefineField(fieldName, fieldType, FieldAttributes.Static | FieldAttributes.Public); values.Add(cachedFormatterField.Name, formatterTask); fieldMap.Add(cachedFormatterField.Name, field); } FieldBuilder keyListFieldBuilder = helperBuilder.DefineField("_fieldIDs", typeof(List <int>), FieldAttributes.Static | FieldAttributes.Public); Type helperType = helperBuilder.CreateType(); foreach (KeyValuePair <string, Task <IFormatter> > pair in values) { FieldInfo realFieldInfo = helperType.GetField(pair.Key); realFieldInfo.SetValue(null, pair.Value); _subFormatters.Add(fieldMap[pair.Key], realFieldInfo); } _keyListField = helperType.GetField(keyListFieldBuilder.Name); List <int> keys = _fields.Select(f => f.ID).ToList(); _keyListField.SetValue(null, keys); }