0

我正在为 asp.net 中可用的所有常见样式类型搜索数据源,并且htmlTextWriterStyle似乎有我可以要求的所有内容。

如果我可以使用其他来源,我会很高兴知道。

我试图实现的是将其值复制到任何其他类型的数据“容器”

这与 enum 不同,更灵活,尽管它的值仍然可以通过

var test = name.value ;

string MyElmWidth = htmlTextWriterStyle.width.ToString();

并且可以像列表一样分配,因此我可以通过 foreach 循环动态填充它

(不必硬编码变量和值)

一个集合或类似代码中的列表:

                public static List<string> EnumToStrLst<T>()
                {
                    Array values = Enum.GetValues(typeof(T));
                    T[] result = (T[])values;
                    List<string> strlst = new List<string>();
                    foreach (var item in result)
                    {
                        string e = item.ToString();

                        strlst.Add(e);
                    }
                    return strlst;
                }

List<string>然后以这种方式填充 a :

List<string> testLstStr = EnumToStrLst<htmlTextWriterStyle>()

虽然这里的整个问题是访问它的值 - 一种不像枚举那样优雅的方式:

testLstStr.elementAt(index) 

反对

enumName.valueName
4

2 回答 2

1

这没有经过测试,但是如何创建这样的包装类:

public class HtmlTextWrapper {

//private members
   private HtmlTextWriterStyle _htmlTextWriterStyle;


//public props same as the wrapped htmltextwriterstyle
    public string BackgroundColor { 
        get
        {
            if (_htmlTextWriterStyle != null)
            {
                return _htmlTextWriterStyle.BackgroundColor;
            }
        }
        set
        {
            if (_htmlTextWriterStyle != null)
            {
                this._htmlTextWriterStyle.BackgroundColor = value;
            }
        }
    }

    public string BackgroundImage {get;set; }
    //....
    //....


//construcor
     public HtmlTextWrapper(HtmlTextWriterStyle other)
     {
            _htmlTextWriterStyle = other;
     }



}

这样,您就可以使用这个包装类

于 2012-10-07T00:01:40.837 回答
0

我可以在不放弃访问便利性的情况下做到这一点的唯一方法是使用密封类和 const 字符串从带有摘要的枚举元数据中复制它(仅用一种类型声明很好地布局)

namespace Style
{
    public sealed class StlProps
    {
    // Summary:
    //     Specifies the HTML backgroundcolor style.
    public const string BgColor = "BackgroundColor",
    //
    // Summary:
    //     Specifies the HTML backgroundimage style.
    BackgroundImage = "BackgroundImage",
    //
    // Summary:
    //     Specifies the HTML bordercollapse style.
    BorderCollapse = "BorderCollapse",
    //
    // Summary:
    //     Specifies the HTML bordercolor style.
    BorderColor = "BorderColor",
    //
    // Summary:
    //     Specifies the HTML borderstyle style.
    BorderStyle = "BorderStyle",
    //
    // Summary:
    //     Specifies the HTML borderwidth style.
    BorderWidth = "BorderWidth",
    //
    // Summary:
    //     Specifies the HTML color style.
    Color = "Color",
    //
    // Summary:
    //     Specifies the HTML fontfamily style.
    FontFamily = "FontFamily",
    //
    // Summary:
    //     Specifies the HTML fontsize style.
    FontSize = "FontSize",
    //
    // Summary:
    //     Specifies the HTML fontstyle style.
    FontStyle = "FontStyle",
    //
    // Summary:
    //     Specifies the HTML fontheight style.
    FontWeight = "FontWeight",
    //
    // Summary:
    //     Specifies the HTML height style.
    Height = "Height",
    //
    // Summary:
    //     Specifies the HTML textdecoration style.
    TextDecoration = "TextDecoration",
    //
    // Summary:
    //     Specifies the HTML width style.
    Width = "Width",
    //
    // Summary:
    //     Specifies the HTML liststyleimage style.
    ListStyleImage = "ListStyleImage",
    //
    // Summary:
    //     Specifies the HTML liststyletype style.
    ListStyleType = "ListStyleType",
    //
    // Summary:
    //     Specifies the HTML cursor style.
    Cursor = "Cursor",
    //
    // Summary:
    //     Specifies the HTML direction style.
    Direction = "Direction",
    //
    // Summary:
    //     Specifies the HTML display style.
    Display = "Display",
    //
    // Summary:
    //     Specifies the HTML filter style.
    Filter = "Filter",
    //
    // Summary:
    //     Specifies the HTML fontvariant style.
    FontVariant = "FontVariant",
    //
    // Summary:
    //     Specifies the HTML left style.
    Left = "Left",
    //
    // Summary:
    //     Specifies the HTML margin style.
    Margin = "Margin",
    //
    // Summary:
    //     Specifies the HTML marginbottom style.
    MarginBottom = "MarginBottom",
    //
    // Summary:
    //     Specifies the HTML marginleft style.
    MarginLeft = "MarginLeft",
    //
    // Summary:
    //     Specifies the HTML marginright style.
    MarginRight = "MarginRight",
    //
    // Summary:
    //     Specifies the HTML margintop style.
    MarginTop = "MarginTop",
    //
    // Summary:
    //     Specifies the HTML overflow style.
    Overflow = "Overflow",
    //
    // Summary:
    //     Specifies the HTML overflowx style.
    OverflowX = "OverflowX",
    //
    // Summary:
    //     Specifies the HTML overflowy style.
    OverflowY = "OverflowY",
    //
    // Summary:
    //     Specifies the HTML padding style.
    Padding = "Padding",
    //
    // Summary:
    //     Specifies the HTML paddingbottom style.
    PaddingBottom = "PaddingBottom",
    //
    // Summary:
    //     Specifies the HTML paddingleft style.
    PaddingLeft = "PaddingLeft",
    //
    // Summary:
    //     Specifies the HTML paddingright style.
    PaddingRight = "PaddingRight",
    //
    // Summary:
    //     Specifies the HTML paddingtop style.
    PaddingTop = "PaddingTop",
    //
    // Summary:
    //     Specifies the HTML position style.
    Position = "Position",
    //
    // Summary:
    //     Specifies the HTML textalign style.
    TextAlign = "TextAlign",
    //
    // Summary:
    //     Specifies the HTML verticalalign style.
    VerticalAlign = "VerticalAlign",
    //
    // Summary:
    //     Specifies the HTML textoverflow style.
    TextOverflow = "TextOverflow",
    //
    // Summary:
    //     Specifies the HTML top style.
    Top = "Top",
    //
    // Summary:
    //     Specifies the HTML visibility style.
    Visibility = "Visibility",
    //
    // Summary:
    //     Specifies the HTML whitespace style.
    WhiteSpace = "WhiteSpace",
    //
    // Summary:
    //     Specifies the HTML zindex style.
    ZIndex = "ZIndex";
    }
}
于 2012-10-07T14:37:44.387 回答