I have code like this:
// lookup the object type, instance passing the data as constructor
//all registered types must implement ctor passed ReadOnlySpan<byte>
public static object InterpretPayload(ReadOnlySpan<byte> bytes)
{
var key = bytes[0];
if (!TypeLookup.ContainsKey(key))
return null;
Type type = TypeLookup[key];
var created = Activator.CreateInstance(type, bytes);
return created;
}
TypeLookup
maps numeric values to class types, in a weird sort of factory method pattern. However when changing my codebase to use ReadOnlySpan
over byte[]
I now get a compiler error that bytes
isn't an object, which it isn't.
Is there another way to do this? I believe Activator
tries to find the best ctor based on what I pass in, seems I need to do this more explicitly. Can I use reflection another way or have I found a case reflection can't emulate calling the ctor directly?
Aucun commentaire:
Enregistrer un commentaire