【问题标题】:Passing Class Information C#传递类信息 C#
【发布时间】:2017-05-05 15:03:32
【问题描述】:

我有一堆代码在做同样的事情,但只做了一些更改......听起来是构建一个为所有代码完成工作的方法的绝佳选择。 我需要使用一个类名,但找不到任何类似的东西让我觉得我应该尝试一下 - 这是最接近的 How to use class name as parameter in 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;
    }

【问题讨论】:

    标签: c# asp.net-mvc-4


    【解决方案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");
    

    我敢打赌,聪明的人会做得更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多