2012-01-17 56 views
1

我有一个方法下如何使下面的代码通用?

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函数作为通用的类名称可以是任何东西?

如何做?

回答

3

尝试以下操作:

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; 
} 
1
private int SaveRecord<T>(T partnerViewLog, PortalConstant.DataSourceType dataSourceType, Func<IDataAccess, T, int> chooseSelector) 
{ 
    ... 
} 

其他每一件事保持不变