2017-05-05 85 views
-2

我有一堆代碼做同樣的事情,但只做了一些改變......聽起來像是一個很好的選擇,爲建立一個方法來爲他們做所有的工作。 我需要使用一個類名,但沒有找到任何足夠simular覺得我應該嘗試一下 - 這是最接近How to use class name as parameter in C#傳遞類信息C#

我的班ImportUserNotificationListModel實現與已經引起了我的一些問題在其他一些領域泛型接口所以這可能會有點困難。

public class ImportNotificationRoleListModel : IImportListBase<ImportNotificationRoleModel>, IImportListDatabaseCalls<ImportNotificationRoleModel> 

public class ImportUserNotificationListModel : IImportListBase<ImportUserNotificationModel>, IImportListDatabaseCalls<ImportUserNotificationModel> 

這裏是我想不重複的代碼:

private static bool SaveUserNotification(XDocument xdoc, int importID, SqlConnection cn, SqlTransaction tran) 
    { 
     try 
     { 

var SourceInfo = xdoc.Root.Element("UserNotifications").ToString(); 
      XmlSerializer serializer = new XmlSerializer(typeof(ImportUserNotificationListModel)); 

      using (TextReader reader = new StringReader(SourceInfo)) 
      { 
       ImportUserNotificationListModel Listresult = (ImportUserNotificationListModel)serializer.Deserialize(reader); 

       foreach (ImportUserNotificationModel lim in Listresult.ImportItems) 
       { 


        Listresult.SaveImportToDatabase(lim, importID, cn, tran); 
       } 

       return true; 

      } 
     } 
     catch (Exception e) 
     { 
      Console.Write(e.Message); 
     } 


     return false; 
    } 

這裏是複製(我有做約12)

private static bool SaveNotificationRoles(XDocument xdoc, int importID, SqlConnection cn, SqlTransaction tran) 
    { 
     try 
     { 

      var SourceInfo = xdoc.Root.Element("NotificationRoles").ToString(); 
      XmlSerializer serializer = new XmlSerializer(typeof(ImportNotificationRoleListModel)); 

      using (TextReader reader = new StringReader(SourceInfo)) 
      { 
       ImportNotificationRoleListModel Listresult = (ImportNotificationRoleListModel)serializer.Deserialize(reader); 

       foreach (ImportNotificationRoleModel lim in Listresult.ImportItems) 
       { 


        Listresult.SaveImportToDatabase(lim, importID, cn, tran); 
       } 

       return true; 

      } 
     } 
     catch (Exception e) 
     { 
      Console.Write(e.Message); 
     } 


     return false; 
    } 

回答

1

編譯:

private static bool Save<T, T2>(XDocument xdoc, 
     int importID, SqlConnection cn, SqlTransaction tran, String elementName) 
    where T: IImportListBase<T2>, IImportListDatabaseCalls<T2> 
    where T2 : IImportBase 
{ 
    try 
    { 
     var SourceInfo = xdoc.Root.Element(elementName).ToString(); 

     XmlSerializer serializer = new XmlSerializer(typeof(T)); 

     using (TextReader reader = new StringReader(SourceInfo)) 
     { 
      T Listresult = (T)serializer.Deserialize(reader); 

      foreach (T2 lim in Listresult.ImportItems) 
      { 
       Listresult.SaveImportToDatabase(lim, importID, cn, tran); 
      } 

      return true; 
     } 
    } 
    catch (Exception e) 
    { 
     Console.Write(e.Message); 
    } 

    return false; 
} 

但你必須明確地給它兩個類型參數的時候你怎麼稱呼它,再加上元素名稱:

Save<ImportNotificationRoleListModel, ImportNotificationRoleModel>(
    null, 0, null, null, "NotificationRoles"); 

Save<ImportUserNotificationListModel, ImportUserNotificationModel>(
    null, 0, null, null, "UserNotifications"); 

我敢打賭,有人聰明可以做的更好。