希望通過一個類的屬性列表,然后為每個傳遞的屬性讀取自定義屬性:class Foo { [FooBar(Name="Prop1")] public string Prop1 { get; set; } [FooBar(Name="Prop2")] public int Prop2 { get; set; } [FooBar(Name="Prop3")] public bool Prop3 { get; set; }}// unsure of the props parameter type herepublic List<string> GetAttr(Expression<List<Func<Foo, object>>> props){ foreach(var prop in props) { // get FooBar attributes name value of the properties passed in }}然后按照以下方式引用:GetAttr(bar => { bar.Prop1, bar.Prop2 });這將返回:"Prop1", "Prop2"我設法通過將參數定義為 params Expression>> 使其工作,但這最終非常冗長,因為每次都需要指定條形引用:GetAttr(bar => bar.Prop1, bar => bar.Prop2);雖然這有效,但它比我試圖替換的系統更冗長。目的是能夠指定列表中返回的屬性編輯:在示例代碼中添加了另一個屬性。
- 1 回答
- 0 關注
- 144 瀏覽
添加回答
舉報
0/150
提交
取消