C# 3.0 匿名类型:命名
我想知道是否有某种方法可以命名或重命名匿名类型的属性以在属性名称中包含空格。 例如:
var resultSet = from customer in customerList
select new
{
FirstName = customer.firstName;
};
在此示例中,我希望 FirstName 为“First Name”。 这个问题的原因是我有一个用户控件公开了一个公共 DataSource 属性,我将其绑定到不同的匿名类型。 它现在工作得很好,除了列名有点不太用户友好(FirstName 而不是 First Name)的一个小缺点。
I was wondering if there is some way to name or rename a property on an Anonymous type to include a space in the property name. For example:
var resultSet = from customer in customerList
select new
{
FirstName = customer.firstName;
};
In this example I would like FirstName to be "First Name". The reason for this question, is I have a user control that exposes a public DataSource property that I bind to different anonymous type. It is working perfectly right now, except for the one little shortcoming of the column names being a little less than user friendly (FirstName instead of First Name).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
做这样的事情怎么样:
然后在您的用户控件中使用值作为内容,标题作为列名。
What about doing something like this:
Then in your user control use Value as the contents and Title as the column name.
不,这是不可能的,成员名称中不允许有空格,您可以使用下划线或在数据绑定后以编程方式更改列的标题...
No, its not possible, spaces are not allowed in the names of members, you can use perhaps underscore or programmatically change the captions of your columns after the data is bound...
我将向属性添加一个属性,您可以在其中指定自定义名称,并且可以使用该属性提供更多用户友好的名称。
I'd add an attribute to the property where you can specify a custom name, and you can provide more user friendly names using the attribute.