【发布时间】:2020-03-18 22:25:16
【问题描述】:
因此,我目前正在将我一直在开发的 VB.NET 应用程序重写为 C#(用于刷新器),但我遇到了一些我似乎无法弄清楚的障碍。我正在尝试遍历对象属性并获取所有属于 CustomList
自定义列表的 VB.NET 代码
Friend Class CustomList(Of T)
Inherits List(Of T)
Private ReadOnly Property FileName As String
Get
Return GetType(T).ToString + ".json"
End Get
End Property
Private ReadOnly Property Campaign As Campaign
Private ReadOnly Property FilePath As String
Get
Return Source.SourcePath + FileName
End Get
End Property
Public Sub New(source As Source)
_Source= source
If IO.File.Exists(FilePath) Then Me.AddRange(JSON_Functions.LoadJSONObject(Of CustomList(Of T))(FilePath))
End Sub
Public Sub New(objList As List(Of T))
Me.AddRange(objList)
End Sub
Public Sub Save()
JSON_Functions.SaveJsonObject(FilePath, Me)
End Sub
End Class
自定义列表的 C# 代码
public class CustomList<T> : List<T>
{
private static string FileName { get { return typeof(T).Name + ".json"; } }
private Source Source { get; set; }
private static string FilePath { get { return Source.Path + FileName; } }
public CustomList(Source source)
{
this.Source = source;
if (File.Exists(FilePath))
this.AddRange(GlobalElements.JSON_Functions.LoadJsonObject<CustomList<T>>(FilePath));
}
public CustomList(List<T> objList) { this.AddRange(objList); }
public void Save() { GlobalElements.JSON_Functions.SaveJsonObject(FilePath, this); }
}
我正在尝试在 CustomList 的每个属性上调用 Save 命令,但在这方面除了死胡同之外什么也没遇到。在 VB.NET 中,我能够以相对简单的方法做到这一点,但在 C# 中,我没有发现任何等效的方法。
我的最终目标是能够通过循环保存 Object1List、Object2List、Object3List 等,而不必单独调用每个属性的 Save 方法。
保存方法的 VB.NET 代码(它可以处理任何通用列表,但我仍然可以调用方法而不将它作为 VB.NETS 对象定义的一部分)
Public ReadOnly Property Object1List As CustomList(Of Object1)
Public ReadOnly Property Object2List As CustomList(Of Object2)
Public ReadOnly Property Object3List As CustomList(Of Object3)
//other code stuffs
Public Sub SaveCampaign()
For Each p As PropertyInfo In Me.GetType.GetProperties
If GetType(IList).IsAssignableFrom(p.PropertyType) AndAlso p.PropertyType.IsGenericType Then p.GetValue(Me).Save()
Next
End Sub
保存方法的 C# 代码
public CustomList<Object1> Object1List { get; private set; }
public CustomList<Object2> Object2List { get; private set; }
public CustomList<Object3> Object3List { get; private set; }
//Other Code Stuff
public void SaveSource()
{
foreach (PropertyInfo p in this.GetType().GetProperties())
{
if (typeof(IList).IsAssignableFrom(p.PropertyType) && p.PropertyType.IsGenericTypeDefinition)
p.GetValue(this).Save();
}
}
这会在p.GetValue(this).Save(); 上返回错误:
'object' does not contain a definition for 'Save' and no accessible extension method 'Save' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
我想要完成此操作的最大原因是我可能有 10 多个 CustomList
如果有人可以提供一些建议,我将不胜感激!!!
【问题讨论】:
-
不太明白你的问题,为什么你有3个属性(Object1List,Object2List,Object3List)并尝试循环访问属性?为什么没有 1 个属性 List
> 并循环遍历这个 List? -
@Alex-TinLe 问题是 Object1、Object2 和 Object3 都是不同的类型。 CustomList
用于保存我给它的任何对象类型的列表。如果我只使用列表中的一种特定类型,则不需要为 CustomList 使用泛型类型。 -
查看我的答案,您可以拥有 1 个通用接口。然后使用 List
. -
@Alex-TinLe 确实让它工作了。非常感谢!!我这样做的目标的根本是消除复制和粘贴代码,并且只是说,“如果你有它们,请保存所有这些”。
标签: c# vb.net properties generic-list