static AvroISpecificRecordSchema()

in src/DotPulsar/Schemas/AvroISpecificRecordSchema.cs [47:79]


    static AvroISpecificRecordSchema()
#pragma warning restore CS8618 // Supressed because if there is an init error the non-static constructor will throw it instead. This is done in case of if there is a wrong implementation of ISpecificRecord in T in order not to stop the whole runtime.
    {
        _typeT = typeof(T);

        try
        {
            if (!_typeT.GetInterfaces().Any(i => i.FullName == AvroISpecificRecordFullName))
                throw new SchemaException($"The type '{_typeT}' must implement '{AvroISpecificRecordFullName}'");

            _avroSchema = _typeT.GetField(SchemaField)?.GetValue(null) ?? throw new SchemaException($"The static field '{SchemaField}' must not be null in type '{_typeT}'");

            var avroSchemaType = _avroSchema.GetType();
            if (!avroSchemaType.ImplementsBaseTypeFullName(AvroSchemaFullName))
                throw new SchemaException($"The static field '{SchemaField}' must be of type '{AvroSchemaFullName}'");

            TryLoadStatic(out Type avroWriterType, out Type avroReaderType, out TypeInfo binaryEncoderType, out TypeInfo binaryDecoderType, out MethodInfo avroWriterMethod, out MethodInfo avroReaderMethod);
            _avroWriterTypeInfo = avroWriterType;
            _avroReaderTypeInfo = avroReaderType;
            _binaryEncoderTypeInfo = binaryEncoderType;
            _binaryDecoderTypeInfo = binaryDecoderType;
            _avroWriterWriteMethod = avroWriterMethod;
            _avroReaderReadMethod = avroReaderMethod;

            var schemaName = (string) (avroSchemaType.GetProperty("Name")?.GetValue(_avroSchema) ?? string.Empty);
            var schemaData = (string) (avroSchemaType.GetMethod("ToString", Type.EmptyTypes)?.Invoke(_avroSchema, null) ?? throw new SchemaException($"Schema 'ToString()' must not return null for type '{_typeT}'"));
            _schemaInfo = new SchemaInfo(schemaName, Encoding.UTF8.GetBytes(schemaData), SchemaType.Avro, new Dictionary<string, string>());
        }
        catch (Exception exception)
        {
            _constructorException = exception;
        }
    }