internal static object ProcessSubRecord(SubRecordOperationType operation, ISubRecordOperationContext context) { var key = new SubRecordProcessKey(context.RecordName, context.SubRecordName, operation); if (!SubRecordProcessingDefinitions.ContainsKey(key)) { throw new ArgumentException($"Unrecognized sub-record operation: {key}"); } if (operation == SubRecordOperationType.Read) { var readContext = (SubRecordReadContext)context; if (readContext.buf.Length < readContext.header.Size) { var buf0 = new byte[readContext.header.Size + REALLOCATION_STEP_SIZE]; Array.Copy(readContext.buf, buf0, readContext.buf.Length); readContext.buf = buf0; } readContext.stream.Read(readContext.buf, 0, readContext.header.Size); } return(SubRecordProcessingDefinitions[key].Invoke(null, new object[] { context })); }
static RawUtils() { var types = typeof(RawUtils).Assembly.GetTypes(); { var q = from type in types let typeAttribute = (HandlesSubRecord)Attribute.GetCustomAttribute(type, typeof(HandlesSubRecord)) where typeAttribute != null from method in type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic) let methodAttribute = (HandlesOperation)Attribute.GetCustomAttribute(method, typeof(HandlesOperation)) where methodAttribute != null select new { typeAttribute, methodAttribute, method } ; foreach (var val in q) { var key = new SubRecordProcessKey(val.typeAttribute.RecordName, val.typeAttribute.SubRecordName, val.methodAttribute.SubRecordOperation); if (SubRecordProcessingDefinitions.ContainsKey(key)) { throw new InvalidOperationException($"Ambiguous Handler: {key}"); } SubRecordProcessingDefinitions.Add(key, val.method); } } { var q = from type in types let typeAttribute = (HandlesRecord)Attribute.GetCustomAttribute(type, typeof(HandlesRecord)) where typeAttribute != null from method in type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic) let methodAttribute = (HandlesOperation)Attribute.GetCustomAttribute(method, typeof(HandlesOperation)) where methodAttribute != null select new { typeAttribute, methodAttribute, method } ; foreach (var val in q) { var key = new RecordProcessKey(val.typeAttribute.RecordName, val.methodAttribute.RecordOperation); if (RecordProcessingDefinitions.ContainsKey(key)) { throw new InvalidOperationException($"Ambiguous Handler: {key}"); } RecordProcessingDefinitions.Add(key, val.method); } } }