2017-05-05 73 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"); 

我敢打赌,有人聪明可以做的更好。