使用自定义类型来替换字符串参数



如果您不需要此问题的背面,请跳过下面的问题。

您在第一个代码块中可以看到,我将常数用作传递给样式字典的参数。

由于我知道(到目前为止),这种类型在.net中大量使用,我想找出,如何指定字典将接受传递的参数为专用类型,而不是string类型。

,这对我来说也是关于类型的另一个小课。

                         //string                //string 
html.StyleDict.Add(Stl.Prop.BgColor, Stl.Vals.Clr.Aquamarine);

直到现在,我一直将它(样式词典)字符串用作参数,因此我可以让它完成工作,同时保持简单...因为我仍然与.net一起新鲜,然后返回然后...当我创建此类课时,我什至无法想到了解types是什么,或者如何与他们一起工作。

so ...至于这种情况,实现和用法应该是一个简单的任务(对于你们中的某些人...实际上大多数人,如果不是全部(:)通过使用自定义类型而不是字符串实现。

问题是:如何创建样式类?

...因此,最终它将传递该C#样式任务的字符串,但是字典将允许您作为style的类型传递参数,这是因为HTML CSS造型是通过背后的代码完成的IS通过字符串值来完成的。虽然我会在填充样式词典时使用自定义样式类。

                     //styleType                //styleType 
html.StyleDict.Add(Stl.Prop.BgColor, Stl.Vals.Clr.Aquamarine);

例如。

这是我用来使元素交替交替的类通过使用生成HTML标记的循环,因此我正在循环柜台以及循环中当前元素的其他样式属性

中的其他样式属性

(有点偏离话题,但我必须举一个用法示例才能使这个问题的含义)

...这是"风格"字典使用的位置。

public sealed class html
{
      //  current Typless Dictionary
    public static Dictionary<string, string> StyleDict = new Dictionary<string, string>();

        // future dictionary (: , naturally it wouldnt work with the parameters as it is now (see relevant Styles Class
    public static Dictionary<Styles.Prop, Styles.Vals> StyleDict = new Dictionary<Stl.Prop, Stl.Vals>();
    public static string DynamicStyle_Generator
    (
        int LoopCounter = -1,
        Dictionary<string, string> StyleAttributeDict = null
    )
    {
        string BaseStyle = "", Terminator = "'", BgCol = "";
        StringBuilder StylerSB = new StringBuilder();

        BgCol = "";
        bool bgcolAlternator;
            if (LoopCounter >= 0)
            {
                LoopCounter++ ;
                bgcolAlternator = (RowCounter % 2) == 0;
                    if (bgcolAlternator)
                        BgCol = "#70878F";
                    else BgCol = "#E6E6B8";
             }
        BaseStyle = string.Format("style='background-color:{0};", BgCol);
        return string.Concat(BaseStyle, StyleDict, Terminator);
    }
        Lsts.StlsDict.Add(Stl.Prop.BgColor, Stl.Vals.Clr.Aquamarine);
        public Dictionary<Stl.Prop, Stl.Vals> StlsDict = new Dictionary<Stl.Prop, Stl.Vals>();

    public static class Styles
    {
        public sealed class Props
        {
            public static string pBgUrl1(string fileName)
            {
                return "url('images/" + fileName + "')";
            }
            /// <summary>
            /// Specifies the HTML backgroundimage style.
            /// </summary>
            public static readonly string BgColor = "Background-Color ",
            /// <summary>
            ///     Specifies the HTML backgroundimage style.
            /// </summary>
            BackgroundImage = "Background-image ",
            /// <summary>
            ///     Specifies the HTML bordercollapse style.
            /// </summary>
            BorderCollapse = "BorderCollapse ",
            /// <summary>
            ///     Specifies the HTML bordercolor style.
            ///</summary>
            BorderColor = "BorderColor ",
            /// <summary>
            ///     Specifies the HTML borderstyle style.
            ///     </summary>        
            BorderStyle = "BorderStyle ",
     }

        public sealed class Vals
        {
            public class fontNames
            {
                public const string Aharoni = "Aharoni",
                                    Andalus = "Andalus",
                                    AngsanaNew = "Angsana New",
                                    AngsanaUPC = "AngsanaUPC",
                                    Aparajita = "Aparajita";
            }

            public class Color
            {
                public const string AliceBlue = "AliceBlue";
                public const string AntiqueWhite = "AntiqueWhite";
                public const string Aqua = "Aqua";
                public const string Aquamarine = "Aquamarine";
                public const string Azure = "Azure";
            }
       }
}

我可以用自己的类型传递的另一种方式(条件是使用同一词典)我唯一能想到的是将设置为参数的默认值

使用
while writing this , i could have thought of this actually 

Dictionary<T,T> StyleDict = new Dictionary<T, T>();

不仅转换为字符串!(不工作)

    public static string DynamicStyle_Generator
    (
        int LoopCounter = -1,
        Dictionary<Styles.Props, Styles.Vals.Color> StyleColor = null
        Dictionary<Styles.Props, Styles.Vals.fontNames> StylefontNames = null
    )
    {

在这里不需要,除非您想要字体,否

DynamicStyle_Generator(counterhere , null, DictionaryOfFonts);

否则

DynamicStyle_Generator(counterhere , DictionartyColors, DictionaryOfFonts);

那么,这将完成其工作

        return string.Concat(BaseStyle, DictionartyColors, DictionaryOfFontsm, Terminator);

在我看来,这就像一个过度工程的情况。System.Web.UI.WebControls.WebControl是有原因公开其Style属性的原因,该属性可以内部与键和值的字符串一起使用。您想提供密钥的常数列表,这是可以的,值的常数列表不会。

c#中的枚举用于表示一组固定的不同值

http://www.codeproject.com/articles/20805/enhancing-c-enums

根据您的评论,我认为这是您要寻找的:http://weblogs.asp.net/ralfw/archive/2006/01/07/434774.aspx

从上面的链接:

public enum Colors
{
   red, blue, green
}
Set<Colors> sc = new Set<Colors>();
sc.Add(Colors.red);
sc.Add(Colors.green);
Set<Colors> sc2 = new Set<Colors>();
sc2.Add(Colors.blue);
sc.Add(sc2); // union
Console.WriteLine(sc); // prints: [red,blue,green]

最新更新