How to store UObject class in TMap for NewObject?

I want to create different UObject with a FName, like ‘tree’ → ‘UTreeData’, ‘sword’ → USwordData.

so, NewObject can create UObject, right?

I think if I make(hardcode) a map that store information from a name to a UObject class ?

base on this idea, I create a TMap<FName, TSubclassOf<UObject>>, and do some hardcode.

TMap<FName, TSubclassOf<UObject>> map;
map.Add("Tree", UTreeData::StaticClass())
map.Add("Sword", USwordData::StaticClass())
...

and then, I think I can create a UObject by a FName.

UObject* createObject(FName name) {
    auto type = map[name];
    return NewObject<type>(this);
}

But compile error, how to fix it?

by the way, I also try to use

TMap<FName, UClass*> // not work
TMap<FName, T> // not work

Template types are resolved at compile time, so you cannot pass a variable there.
NewObject doesn’t seem to provide genericity like SpawnActor does, so you’ll have to shortcut it :

auto type = map[name];
//this is NewObject code
FStaticConstructObjectParameters Params(type);
Params.Outer = this;
return StaticConstructObject_Internal(Params);