C#:为什么我的字符串 return 对象类型而不是它包含的值?

C#: Why does my string return object type and not the value it contains?

我正在遍历 List,并尝试将其中一个属性实例化为字符串,但 return 的类型是:

{Namespace.Collection}

如果我放置一个断点,我可以看到它拥有我需要的值。

如何使它成为 return 值而不是类型?

foreach (var PropertyName in ListName) {
    string n = PropertyName.ToString();
}

更新(添加了更多我的代码,以及尝试实施建议的解决方案):

foreach (DataRow dr in ds.Tables[0].Rows) {

//PaidTrips is my ObservableCollection instance.
//PaidTrip is my holder class which it has been bound to.

                        PaidTrips.Add(new PaidTrip {

                            LicenseHolderID = dr[0].ToString(),
                            // adding more properties
                        });

                        List<PaidTrip> theseTrips = PaidTrips
                            .GroupBy(p => new { p.LicenseHolderID })
                            .Select(g => g.First())
                            .ToList();
                       
                        foreach (PaidTrip PaidTrips in theseTrips) {

                            foreach (var LicenseHolderID in PaidTrips.GetType().GetProperties()) {

                                string n = LicenseHolderID.GetValue(PaidTrips).ToString();

// code to create PDF
}

gfx.DrawString(n, new XFont("Arial", 40, XFontStyle.Bold), ridelGreen, new XPoint(40, 350));

这就是我对 string n 所做的。但是创建PDF时,字符串输出为System.Action1[System.Action]`

我做错了什么?

对于你的问题 - 我猜想 ListName 不是 string 类型,所以你得到了预期的行为(参见:https://docs.microsoft.com/en-us/dotnet/api/system.object.tostring?view=net-5.0#the-default-objecttostring-method

在那种情况下,您可以将该对象的 ToString() 函数重写为 return 任何您需要的,如下所示:

   public override string ToString()
   {
      return "whatever you want including class properties";
   }

另一方面,C# 中变量命名的一般方法是驼峰式命名并且以小写字母开头,因此我建议将变量命名为 propertName 而不是 PropertyNamelistName ListName.

此外 - 根据变量的实现方式命名变量 (ListName) 不是最佳实践,因为它将它们绑定在一起,不允许在实现更改时灵活(该评论只有在有意义的情况下才是正确的,因为我没有看到所有代码)

干杯

您需要在遍历列表后遍历自定义 class 中的 属性 类型。首先我们需要一个额外的循环 - 遍历 ListName 列表中的每个 ClassName 对象。

            foreach (ClassName myObj in ListName)
            {
                foreach (var PropertyName in myObj.GetType().GetProperties())
                {
                    string n = PropertyName.GetValue(myObj).ToString();
                }
            }

然后我们需要循环当前循环ClassName对象的实际属性

然后传递参数 .GetValue(因为您现在正在遍历属性 - 实际分配的属性,而不是属性的定义)。

之后,您还需要指定您想要的对象的值。因此,通过传递 myObj,您指定了 ListName.

当前循环的 ClassName->属性

编辑:


            List<Notes> myNotesNow = new List<Notes>();

            myNotesNow.Add(new Notes
            {
                note1 = "Valuye"
                // adding more properties
            });

            List<Notes> theseTrips = myNotesNow;

            foreach (Notes PaidTrips in theseTrips)
            {

                foreach (var myVariable in PaidTrips.GetType().GetProperties())
                {

                    string n = myVariable.GetValue(PaidTrips).ToString();

                    string forBreakPoint = "";
                    // code to create PDF
                }

            }