C# PropertyInfo.GetValue()(当属性是 SortedSet 时)<T>

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

更新:我知道我提供的信息有点缺陷,所以我编辑了问题。然而,当属性是 SortedSet 时,如何从属性中获取值呢?当我尝试迭代 GetValue() 的返回值时,它给了我一个参数空异常。

当所述属性是

PropertyInfo.Name
时,是否有一种实用的方法可以从类实例中获取
PropertyInfo.GetValue()
SortedSet<T>
?例如:

机器类如下所示:

public class Machine : IComparable, IComparable<Machine>
{
    [BsonId]
    public Guid Id { get; set; }

    [BsonElement]
    public SortedSet<SubAssembly> SubAssemblies { get; set; } = [];
}

然后,我从数据库中提取所有

Machines
并尝试获取属性名称和值,如下所示:

 foreach (var MachineInDB in db.GetRecordList<Machine>(MachinesTable))
 {
     PropertyInfo[] MachineClassProperties = MachineInDB.GetType().GetProperties();

     Console.WriteLine("\n");

     foreach (PropertyInfo MachineProperty in MachineClassProperties)
     {
         
          Console.WriteLine($"{MachineProperty.Name} => {MachineProperty.GetValue(MachineInDB)}");
            
          //this one will print the following when it gets to the problem property
          //SubAssemblies => System.Collections.Generic.SortedSet`1[SubAssembly]

          //If I try to iterate over the SortedSet property, I get an ArgumentNull exception. 
         
     }
 }

使用上述代码,类中的所有其他属性都可以正常打印。如果我使用

Machine
从数据库中打印每个
ToString()
,则所有
SubAssemblies
都会完美打印,因此从数据库中提取数据没有问题。

不知何故,当它尝试仅在“SortedSet”上使用

PropertyInfo
时,
reflectance
元素为空。有谁知道如何解决这个问题?我敢打赌,如果我切换到
List<T>
它会工作得很好,但我真的需要它成为
SortedSet<T>

我还尝试将返回值放入“var”中并对其进行迭代,但最终出现了

ArgumentNull
异常。这就是我知道它只是没有使用
GetValue()
方法拉动对象。

期望“SortedSet”对象是错误的吗?正确的做法是什么?

谢谢!

BMET1

c# system.reflection sortedset propertyinfo
1个回答
0
投票

您甚至不需要知道要使用的类型

GetValue
,因为它可以将其视为对象。我写这篇文章是为了快速测试它。

var m = new Machine();
m.SubAssemblies.Add(new SubAssembly("a"));
m.SubAssemblies.Add(new SubAssembly("c"));
m.SubAssemblies.Add(new SubAssembly("b"));

var machineType = m.GetType(); // or typeof(Machine)
var pi = machineType.GetProperty(nameof(Machine.SubAssemblies));

var value = pi.GetValue(m);

Console.WriteLine(value.ToString());

public class Machine : IComparable, IComparable<Machine>
{
    public Guid Id { get; set; }
    public SortedSet<SubAssembly> SubAssemblies { get; set; } = new();

    public Machine(Guid? id = null)
    {
        Id = id ?? Guid.NewGuid();
    }

    public int CompareTo(Machine other) => Id.CompareTo(other.Id);
    public int CompareTo(object obj) => throw new NotImplementedException();

    public override string ToString() => JsonConvert.SerializeObject(this, Formatting.Indented);
}

public class SubAssembly : IComparable, IComparable<SubAssembly>
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    
    public SubAssembly(string name, Guid? id = null)
    {
        Id = id ?? Guid.NewGuid();
        Name = name;
    }
    
    public int CompareTo(SubAssembly other) => Id.CompareTo(other.Id);
    public int CompareTo(object obj) => throw new NotImplementedException();

    public override string ToString() => JsonConvert.SerializeObject(this, Formatting.Indented);
}

输出:

System.Collections.Generic.SortedSet`1[SubAssembly]

值得注意的是,您可以保留

value
隐式
var
,或者如果您想指定类型,您可以使用任何已实现的接口或
SortedSet<T>
的父类,您可以在 here 查看它们。

  • ISet<T>
  • ICollection<T>
  • IEnumerable<T>
  • IEnumerable
  • ICollection
  • IReadOnlyCollection<T>
  • IReadOnlySet<T>
  • ISerializable
  • IDeserializationCallback
© www.soinside.com 2019 - 2024. All rights reserved.