我如何在自定义List 上从属性调用方法?

问题描述 投票:0回答:1

因此,我目前正在重写我一直在使用C#(用于更新程序)的VB.NET应用程序,但遇到了一些似乎无法弄清的障碍。我试图遍历对象的属性,并获取CustomList 类型的所有属性,然后在每个属性上运行Save方法。

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

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

因此,我目前正在重写我一直在使用C#(用于更新程序)的VB.NET应用程序,但遇到了一些似乎无法弄清的障碍。我正在尝试循环浏览...

c# vb.net properties generic-list
1个回答
0
投票

其中一个选项是

© www.soinside.com 2019 - 2024. All rights reserved.