List<Object> testimonials = new List<Object>();
testimonials.Add(new {
Author = "Author 1",
Testimonial = "Testimonial 1"
});
testimonials.Add(new {
Author = "Author 2",
Testimonial = "Testimonial 2"
});
testimonials.Add(new {
Author = "Author 3",
Testimonial = "Testimonial 3"
});
@ObjectInfo.Print(testimonials[DateTime.Now.DayOfYear % testimonials.Count].Author)
给我一个错误 CS1061:“对象”不包含“作者”的定义
如何从推荐列表中仅获取作者或推荐?
最佳答案
一种懒惰的方法是将“对象”切换为“动态”。或者使用 A Tuple 通用类型。
但是在我看来,您应该简单地编写一个具有两个属性的类:
public class Testimonial {
public string Author {get;set;}
public string Comment {get;set;}
}
并使用推荐列表。
另一种方法是使用类似的东西:
var arr = new[]{new{...},new{...}};
这是一个匿名类型的数组,并且;
string author = arr[0].Author;
可以正常工作。
关于c# - 如何访问匿名类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4172350/