private void CreateHelperClass() { TypeBuilder helperBuilder = GeneratedAssembly.CreateType(TargetType.Name + "FormatterHelper"); FieldBuilder fieldBuilder = helperBuilder.DefineField(_helperFormatterField, UnderlyingFormatter.GetType(), FieldAttributes.Public | FieldAttributes.Static); Type helperType = helperBuilder.CreateType(); UnderlyingFormatterField = helperType.GetField(_helperFormatterField); UnderlyingFormatterField.SetValue(null, UnderlyingFormatter); }
private void CreateTargetTypeField() { TypeBuilder helperBuilder = GeneratedAssembly.CreateType(TargetType + "FormatterHelper"); FieldBuilder field = helperBuilder.DefineField("_targetType", typeof(Type), FieldAttributes.Static | FieldAttributes.Public); Type helperClass = helperBuilder.CreateType(); _targetTypeField = helperClass.GetField(field.Name); _targetTypeField.SetValue(null, TargetType); }
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); }