驼峰拆分 [英] Splitting CamelCase

查看:128
本文介绍了驼峰拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是所有asp.net C#。

This is all asp.net c#.

我有一个枚举

public enum ControlSelectionType 
{
    NotApplicable = 1,
    SingleSelectRadioButtons = 2,
    SingleSelectDropDownList = 3,
    MultiSelectCheckBox = 4,
    MultiSelectListBox = 5
}

此的数字值被存储在我的数据库。我在DataGrid中显示该值。

The numerical value of this is stored in my database. I display this value in a datagrid.

<asp:boundcolumn datafield="ControlSelectionTypeId" headertext="Control Type"></asp:boundcolumn>

该ID意味着什么给用户的,所以我已经改变了绑定列用下面的模板列。

The ID means nothing to a user so I have changed the boundcolumn to a template column with the following.

<asp:TemplateColumn>
    <ItemTemplate>
        <%# Enum.Parse(typeof(ControlSelectionType), DataBinder.Eval(Container.DataItem, "ControlSelectionTypeId").ToString()).ToString()%>
    </ItemTemplate>
</asp:TemplateColumn>

这是好了很多。不过,这将是巨大的,如果有一个简单的功能,我可以把周围的枚举由骆驼情况下,分割,这样的话在DataGrid包装精美。

This is a lot better... However, it would be great if there was a simple function I can put around the Enum to split it by Camel case so that the words wrap nicely in the datagrid.

请注意:本人完全清楚,有做这一切的更好的方法。该屏幕纯粹是内部使用,我只是想在一个地方快速劈好一点显示出来。

Note: I am fully aware that there are better ways of doing all this. This screen is purely used internally and I just want a quick hack in place to display it a little better.

推荐答案

事实上正则表达式/替换是去为对方的回答说明,但是这也可能是使用你的,如果你想要去一​​个不同的方式方向

Indeed a regex/replace is the way to go as described in the other answer, however this might also be of use to you if you wanted to go a different direction

    using System.ComponentModel;
    using System.Reflection;

...

    public static string GetDescription(System.Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());
        DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }

这将允许您定义枚举为

public enum ControlSelectionType 
{
    [Description("Not Applicable")]
    NotApplicable = 1,
    [Description("Single Select Radio Buttons")]
    SingleSelectRadioButtons = 2,
    [Description("Completely Different Display Text")]
    SingleSelectDropDownList = 3,
}

<一个href=\"http://www.$c$cguru.com/forum/archive/index.php/t-412868.html\">http://www.$c$cguru.com/forum/archive/index.php/t-412868.html

这篇关于驼峰拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆