I have a method that must return a generic interface. Have tried to make the method in delphi. But are unsure on how it should be written? Is there anyone who can help me?
Here's an example I've made in Java that works:
public < T extends StandardDataProvider>> T GetDataProvider(String dataProviderName) {
if (dataproviders == null)
buildDataProviderMap();
if (dataproviders.containsKey(dataProviderName)) {
return (T) dataproviders.get(dataProviderName);
} else
return null;
}
Then tried to do the same in delphi .. But can not get it to work?
function TLocalDataProviderFactory. GetDataProvider(DataProviderName: string): IStandardDataProvider; // Shows errors here?
begin
if not Assigned(DataProvider) then
BuildDataProviderMap;
if DataProvider.ContainsKey(DataProviderName) then
begin
Result := DataProvider.Items[DataProviderName];
end
else
begin
Result:= nil;
end;
end;
解决方案
Delphi generic constraints do not support wildcards. So the closest you can manage involves two generic parameters. The function would look like this:
function GetDataProvider>(...): T;