The thing you ask for is as if you were asking for designate a cat as a dog the same time like in a schrodinger box.
However, you have the notion of dynamic but this does not apply to foreach because you can't assign the iteratored var and here the goal is not to modulate it.
Therefore you need to write:
foreach (string layerName in ObstacleLayers)
{
var layer = _tiledMap.GetLayer<TiledMapTileLayer>(layerName);
// do something with the layer
}
So the string element you iterate over from the collection is used to get an object instance of type Layer
(perhaps) to work on.
It seems that you did not understand or learned what are loops and variables and their usage as well as scope.