2017-05-05 13 views
-2

同じことをやっているが、ほんの少しの変更を加えたコードがたくさんある...それらのすべての作業を行うメソッドを構築するための素晴らしいオプションのように聞こえる。これは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"); 

関連する問題