gpt4 book ai didi

c# - 如何模拟多重继承并使用反射来优化此代码?

转载 作者:行者123 更新时间:2023-12-03 19:20:30 25 4
gpt4 key购买 nike

我有一个 WPF 应用程序,其中 PageItems 是模型对象。

我的主 ViewModel 有一个 PageItemViewModels 的 ObservableCollection,每个模型都根据其匹配的 PageItem 模型对象构建自身。

每个 PageItemViewModel 都继承自抽象类 BaseViewModel,以获得 INotifyPropertyChanged 功能。

每个 PageItemViewModel 还实现 IPageItemViewModel 以确保它具有所需的属性。

我最终会有大约 50 页,所以我想消除任何不必要的代码:

  • 已解决(见下文):有没有办法让 PageItemViewModel 类继承 IdCode 和 Title,这样我就不必在每个类中实现它们?我无法将它们放入 BaseViewModel 中,因为其他 ViewModel 继承了它,不需要这些属性,并且我无法将它们放入 IPageItemViewModel 中,因为它只是一个接口(interface)。我知道我需要多重继承,而 C# 不支持
  • 已解决(见下文):有没有办法可以摆脱 switch 语句,例如不知何故使用反射代替?

下面是一个独立控制台应用程序,它演示了我的WPF应用程序中的代码:

using System.Collections.Generic;

namespace TestInstantiate838
{
public class Program
{
static void Main(string[] args)
{
List<PageItem> pageItems = PageItems.GetAll();
List<ViewModelBase> pageItemViewModels = new List<ViewModelBase>();

foreach (PageItem pageItem in pageItems)
{
switch (pageItem.IdCode)
{
case "manageCustomers":
pageItemViewModels.Add(new PageItemManageCustomersViewModel(pageItem));
break;
case "manageEmployees":
pageItemViewModels.Add(new PageItemManageEmployeesViewModel(pageItem));
break;
default:
break;
}
}
}
}

public class PageItemManageCustomersViewModel : ViewModelBase, IPageItemViewModel
{
public string IdCode { get; set; }
public string Title { get; set; }

public PageItemManageCustomersViewModel(PageItem pageItem)
{

}
}

public class PageItemManageEmployeesViewModel : ViewModelBase, IPageItemViewModel
{
public string IdCode { get; set; }
public string Title { get; set; }

public PageItemManageEmployeesViewModel(PageItem pageItem)
{

}
}

public interface IPageItemViewModel
{
//these are the properties which every PageItemViewModel needs
string IdCode { get; set; }
string Title { get; set; }
}

public abstract class ViewModelBase
{
protected void OnPropertyChanged(string propertyName)
{
//this is the INotifyPropertyChanged method which all ViewModels need
}
}

public class PageItem
{
public string IdCode { get; set; }
public string Title { get; set; }
}

public class PageItems
{
public static List<PageItem> GetAll()
{
List<PageItem> pageItems = new List<PageItem>();
pageItems.Add(new PageItem { IdCode = "manageCustomers", Title = "ManageCustomers"});
pageItems.Add(new PageItem { IdCode = "manageEmployees", Title = "ManageEmployees"});
return pageItems;
}
}

}

重构:接口(interface)改为抽象类

using System;
using System.Collections.Generic;

namespace TestInstantiate838
{
public class Program
{
static void Main(string[] args)
{
List<PageItem> pageItems = PageItems.GetAll();
List<ViewModelPageItemBase> pageItemViewModels = new List<ViewModelPageItemBase>();

foreach (PageItem pageItem in pageItems)
{
switch (pageItem.IdCode)
{
case "manageCustomers":
pageItemViewModels.Add(new PageItemManageCustomersViewModel(pageItem));
break;
case "manageEmployees":
pageItemViewModels.Add(new PageItemManageEmployeesViewModel(pageItem));
break;
default:
break;
}
}

foreach (ViewModelPageItemBase pageItemViewModel in pageItemViewModels)
{
System.Console.WriteLine("{0}:{1}", pageItemViewModel.IdCode, pageItemViewModel.Title);
}
Console.ReadLine();
}
}

public class PageItemManageCustomersViewModel : ViewModelPageItemBase
{
public PageItemManageCustomersViewModel(PageItem pageItem)
{
IdCode = pageItem.IdCode;
Title = pageItem.Title;
}
}

public class PageItemManageEmployeesViewModel : ViewModelPageItemBase
{
public PageItemManageEmployeesViewModel(PageItem pageItem)
{
IdCode = pageItem.IdCode;
Title = pageItem.Title;
}
}

public abstract class ViewModelPageItemBase : ViewModelBase
{
//these are the properties which every PageItemViewModel needs
public string IdCode { get; set; }
public string Title { get; set; }
}

public abstract class ViewModelBase
{
protected void OnPropertyChanged(string propertyName)
{
//this is the INotifyPropertyChanged method which all ViewModels need
}
}

public class PageItem
{
public string IdCode { get; set; }
public string Title { get; set; }
}

public class PageItems
{
public static List<PageItem> GetAll()
{
List<PageItem> pageItems = new List<PageItem>();
pageItems.Add(new PageItem { IdCode = "manageCustomers", Title = "ManageCustomers"});
pageItems.Add(new PageItem { IdCode = "manageEmployees", Title = "ManageEmployees"});
return pageItems;
}
}

}

消除 Switch 语句的答案:

谢谢贾布:

string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
string viewModelName = assemblyName + ".ViewModels.PageItem" + StringHelpers.ForcePascalNotation(pageItem.IdCode) + "ViewModel";
var type = Type.GetType(viewModelName);
var viewModel = Activator.CreateInstance(type, pageItem) as ViewModelBase;
AllPageViewModels.Add(viewModel);

最佳答案

您能否创建一个继承自 BaseViewModel 的类来实现这两个属性 - 然后您需要此属性的 PageItemViewModel 类可以继承该类。

关于c# - 如何模拟多重继承并使用反射来优化此代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1027445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com