1
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が表示されます:「オブジェクト」に「作成者」の定義が含まれていません

推薦状のリストから著者または推薦状のみを取得するにはどうすればよいですか?

4

2 に答える 2

5

怠惰な方法は、「オブジェクト」を「動的」に切り替えることです。または、タプルジェネリック型を使用します。

ただし、IMOでは、次の2つのプロパティを聞くだけのクラスを作成する必要があります。

public class Testimonial {
    public string Author {get;set;}
    public string Comment {get;set;}
}

そして、推薦状のリストを使用します。

別の方法は、次のようなものを使用することです。

var arr = new[]{new{...},new{...}};

これはあなたのanon-typeの配列です、そして;

string author = arr[0].Author;

うまくいくだろう。

于 2010-11-13T11:50:45.030 に答える
0

暗黙的に型指定された配列を使用します。

var arr = new[]
{
    new { Author = "Author 1", Testimonial = "Testimonial 1" },
    new { Author = "Author 2", Testimonial = "Testimonial 2" },
    new { Author = "Author 3", Testimonial = "Testimonial 3" }
};
// .ToList() if needed, however array supports indexer

string author = arr[i].Author;
于 2010-11-13T12:16:28.697 に答える