how to pass dynamic class to generic function

You can use the non-templated CreateDefaultSubobject version (which is called by the templated one) that takes additional const UClass* ReturnType, const UClass* ClassToCreateByDefault parameters. ReturnType would be UMeshComponent::StaticClass() and ClassToCreateByDefault would be *MeshClass in your example.

If you’re using FObjectInitializer you can call SetDefaultSubobjectClass to set the override class to create instead. This is typically used directly in the subclass.