【发布时间】: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