It seems like no. I have a utility method
public static Map<String, SObject> getMapOfStringToSObject(List<SObject> lstSObjs, String strKeyFieldName)
{
Map<String, SObject> mapStringToSObject = new Map<String, SObject>();
for (SObject so : lstSObjs)
mapStringToSObject.put((String)so.get(strKeyFieldName), so);
return mapStringToSObject;
}
But when I try to use it (with casting) I get System.TypeException: Invalid conversion from runtime type Map<String, SObject> to Map<String, MyObject> ()
Client code is
Map<String, MyObject> res = (Map<String, MyObject>)UtilsClass.getMapOfStringToSObject(someList, 'someStringField');
Sure I could not cast and loop over the results. But the whole point of this is to abstract away the looping and not have to write it over and over. I have similar methods (where I can cast successfully) that return a variety of types including among others
Map<String, List<SObject>>
Map<Id, List<SObject>>
MyObject
in your example code an apex class type?