如何从 属性 调用自定义列表 <T> 上的方法?

How can I call method on custom List<T> from a property?

所以我目前正在重写一个 VB.NET 应用程序,我一直在用 C# 开发它(为了复习),我 运行 遇到了一些障碍,我似乎无法弄清楚。我正在尝试循环访问对象属性并获取类型为 CustomList 的所有属性和 运行 每个对象的 Save 方法。

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 的每个 属性 上调用保存命令,但除了死胡同外什么也没遇到。在 VB.NET 中,我能够以相对简单的方法完成此操作,但在 C# 中,我没有发现任何等效方法。

我的最终目标是能够通过循环保存 Object1List、Object2List、Object3List 等,而不必单独调用每个属性的 Save 方法。

VB.NET Save 方法的代码(这处理任何通用列表,但我仍然能够调用一个方法,而无需将其与 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(); 上的 returns 错误:

    '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 并且不想每次都列出它们。

如果有人能提供一些建议,我将不胜感激!!!

其中一个选项是

public interface IMyCustomList
{
    void Save();
}

public class CustomList<T> : List<T>, IMyCustomList
{ .... }

然后将你的属性值投射到接口

var propValue = p.GetValue(this);
if (propValue != null && propValue is IMyCustomList myList)
    myList.Save();

但是,我仍然认为像这样遍历属性是不好的。