我有一种方法如下
private int SaveRecord(PartnerViewLog partnerViewLog, PortalConstant.DataSourceType DataSourceType, Func<IDataAccess, PartnerViewLog, int> chooseSelector)
{
int results = -1;
var dataPlugin = DataPlugins.FirstOrDefault(i => i.Metadata["SQLMetaData"].ToString() == DataSourceType.EnumToString());
if (dataPlugin != null)
{
results = chooseSelector(dataPlugin.Value, partnerViewLog);
}
return results;
}
我正在调用它
public int SavePartnerViewLog(PartnerViewLog partnerViewLog, PortalConstant.DataSourceType DataSourceType)
{
return SaveRecord(partnerViewLog, DataSourceType, (i, u) => i.SavePartnerViewLog(partnerViewLog));
}
可以看出,PartnerViewLog是一个类。现在,我想使函数SaveRecord成为泛型,其中类名可以是任何名称?
怎么做?
最佳答案
请尝试以下操作:
private int SaveRecord<T>(T record, PortalConstant.DataSourceType dataSourceType, Func<IDataAccess, T, int> chooseSelector)
{
int results = -1;
var dataPlugin = DataPlugins.FirstOrDefault(i => i.Metadata["SQLMetaData"].ToString() == dataSourceType.EnumToString());
if (dataPlugin != null)
{
results = chooseSelector(dataPlugin.Value, record);
}
return results;
}