-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;
}