【问题标题】:How can I call method on custom List<T> from a property?如何从属性调用自定义 List<T> 上的方法?
【发布时间】:2020-03-18 22:25:16
【问题描述】:

因此,我目前正在将我一直在开发的 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 的每个属性上调用 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


【解决方案1】:

其中一个选项是

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();

但是,我认为循环遍历这样的属性并不好。

【讨论】:

  • 为什么不觉得不好?在我看来,在需要保存嵌套对象时使用反射是有意义的。
  • 通常,我们通过使用合约来决定要保存的“什么”(在我的代码中,它是一个接口)。这就是为什么我只会选择 1 个 List 属性。在您的代码中,您只需读取所有属性并尝试将它们与某些条件匹配。假设,如果您的一个队友添加了另一个与您的条件匹配的属性,但他不知道该保存方法。现在,您需要时刻关注这些。
  • 这很公平。这只是我正在进行的一个个人项目,因此目前没有其他人参与其中。一旦我深入研究该项目,可能有理由对其进行重构,但现在这非常适合我的需求。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 2019-10-06
相关资源
最近更新 更多