五一献礼(二) Enum.MM|Enum.GG 这样的Enum你喜欢么?

猪正要去超市准备购物.....这是上一篇

MM:~~猪猪你那个设计我交给老师看了,麻烦你给我在解释下吧

猪:不是把那我怎么办。

MM:晚上演猪的世界~~一起去看

猪:好说好说,你想知道什么呢(心肝)?

MM:仔细说说StyleEnumBase我看不懂

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

//向猪一样飞翔
namespace SeemPigAlikeVolitation
{
 
  

    
public abstract class StyleEnumBase:System.Configuration.Provider.ProviderBase
    
{
        
public static System.Collections.Generic.Dictionary<SeemPigAlikeVolitation.Style, SeemPigAlikeVolitation.StyleEnumBase> MeStyleAll = new System.Collections.Generic.Dictionary<SeemPigAlikeVolitation.Style, SeemPigAlikeVolitation.StyleEnumBase>();

        
public static SeemPigAlikeVolitation.ConfingPage Settings = System.Configuration.ConfigurationManager.GetSection("StyleEnum"as SeemPigAlikeVolitation.ConfingPage;

       
        
//public static StyleCollection MeStyleAll = new StyleCollection();

        
/// <summary>
        
/// 存放日期对应的函数
        
/// </summary>

        public System.Collections.Generic.Dictionary<NeedHelp, string> asOneStyle = new Dictionary<NeedHelp, string>();




        
/// <summary>
        
/// 检索配置节点
        
/// </summary>

        public  static void Instance()
        
{
            MeStyleAll.Clear();
            
//---配置节点中得到数据分隔
            foreach (string ClassName in Settings.StyleNames.Split(','))
            
{

               MeStyleAll.Add((Style)Enum.Parse(
typeof(Style), ClassName), System.Web.Configuration.ProvidersHelper.InstantiateProvider(Settings.StyleClass[ClassName],
                
typeof(StyleEnumBase)) as StyleEnumBase
                );
            }

        }





        
public StyleEnumBase()
        
{
            asOneStyle.Add(NeedHelp.Color,
this.GetColor);
            asOneStyle.Add(NeedHelp.FontSize,
this.GetFontSize);
           
        }




        
/// <summary>
        
/// 得到选择的样式
        
/// </summary>
        
/// <param name="MyDay">输入日期</param>
        
/// <param name="MeStyle">输入选择的样式</param>
        
/// <returns></returns>

        public static System.Collections.Generic.List<string> ToStyle(System.Collections.Generic.List<CommandNeed> TempNeed)
        
{
            System.Collections.Generic.List
<string> ren = new List<string>();
   
//<StyleEnum StyleNames="Read">//Read是表识下面的add name的有多个可以用,号分隔且最后不能出现,
  
//  <StyleClass>
            
//    <add name="Read" type="SeemPigAlikeVolitation.StyleRead" />//这个name在 Style中有对应的枚举阿~~不要搞错了~~最后没有加入错误处理
  
//  </StyleClass>
  
//</StyleEnum>
            
//MeStyleAll.Clear();



            
if (MeStyleAll.Count == 0)
            
{

                Instance();
            }

           
     

            
foreach (CommandNeed onlayNeed in TempNeed)
            
{
                
foreach (Style onlay in MeStyleAll.Keys)
                
{
                    
//--代表选中了相应的样式
                    if ((onlayNeed.WhatStyle  & onlay) != 0)
                    
{
                        
string WriteStyle = "<span  style=\""

                        
foreach (NeedHelp onlayNeedHelp in MeStyleAll[onlay].asOneStyle.Keys)
                        
{
                            
if ((onlayNeed.WhatHelp& onlayNeedHelp) != 0)
                            
{
                                WriteStyle 
+= MeStyleAll[onlay].asOneStyle[onlayNeedHelp].Replace(";"""+";";
                            }

                        }


                        
//--鉴于鄙人订立的规则只能有一个种类样式存在
                       ren.Add(WriteStyle+"\">" + onlayNeed.Info + "</span>");
                        break;
                    }


                }

                    
            }

            
            
return ren;
        }





        
public struct CommandNeed
        
{
            
/// <summary>
            
///输入您需要的样式的种类
            
/// </summary>

            public Style WhatStyle;

            
/// <summary>
            
/// 输入您需要的样式的具体的某1项或者是几项
            
/// </summary>

            public NeedHelp  WhatHelp;
            
public string Info;
            
        }

        
internal abstract string GetColor
        
{
            
get;
        }


        
internal abstract string GetFontSize
        
{
            
get;
        }


      


    }



    
/// <summary>
    
/// 这个不具备旗标记
    
/// </summary>

    [Flags]
    
public enum NeedHelp
    
{
        Color
=0x0003,
        FontSize 
= 0x0004
    }




/// <summary>
///样式 ,要想用|则必须加入旗标记
/// </summary>

    public enum Style
    
{
        Read 
= 0x0001,
        StyleT 
= 0x0002
    }


}



猪:好好这就开始首先StyleEnumBase-就是要反射的类的基础类,你要反射的类就是继承自这个

MM:等等你当我真不懂啊你在web.confing中没有验证派生类是否是StyleEnumBase

猪:阿- -我就这次偷懒还被你发现了艾~~好好补充(My的苍天啊)

天使的声音:别听他们胡扯-请各位在 ProviderSettingsValidation 类增加代码
 public override void Validate(object value)
        
{
            ProviderSettingsCollection providerCollection 
= value as ProviderSettingsCollection;
            
if (providerCollection != null)
            
{

                
foreach (ProviderSettings _provider in providerCollection)
                
{

                    
if (String.IsNullOrEmpty(_provider.Type))
                    
{
                        
throw new ConfigurationErrorsException("Type was not defined in the provider");
                    }


                    Type dataAccessType 
= Type.GetType(_provider.Type);
                    
if (dataAccessType == null)
                    
{
                        
throw (new InvalidOperationException("Provider's Type could not be found"));
                    }


                    
if (typeof( SeemPigAlikeVolitation.StyleEnumBase) != dataAccessType.BaseType)
                    
{
                        
throw (new InvalidOperationException("this type Error "));
                    }

                }

            }

        }

猪:最后一个if就是判断是否是SeemPigAlikeVolitation.StyleEnumBase派生的~~艾咱们继续
StyleEnumBase

        public static System.Collections.Generic.Dictionary<SeemPigAlikeVolitation.Style, SeemPigAlikeVolitation.StyleEnumBase> MeStyleAll = new System.Collections.Generic.Dictionary<SeemPigAlikeVolitation.Style, SeemPigAlikeVolitation.StyleEnumBase>();

        
public static SeemPigAlikeVolitation.ConfingPage Settings = System.Configuration.ConfigurationManager.GetSection("StyleEnum"as SeemPigAlikeVolitation.ConfingPage;

       
        
//public static StyleCollection MeStyleAll = new StyleCollection();

        
/// <summary>
        
/// 存放日期对应的函数
        
/// </summary>

        public System.Collections.Generic.Dictionary<NeedHelp, string> asOneStyle = new Dictionary<NeedHelp, string>();

猪:第一行就是MeStyleAll泛型存储SeemPigAlikeVolitation.Style, SeemPigAlikeVolitation.StyleEnumBase,就是一个样
式匹配一个,派生类
第二行Settings 反射出一个处理节点类~~就是下面Web.confing中设置的
<?xml version="1.0"?>

<configuration>
  
<configSections>
    
<section name="StyleEnum" type="SeemPigAlikeVolitation.ConfingPage, __code" requirePermission="false"/>
  
</configSections>
第三行asOneStyle代表自己处理的样式是以  样式枚举,样式字符串的形式表达的

MM:哦这个就是泛型阿看来是 key,value的形式存储的

猪:恩聪明啊~~  
Instance()函数是用来反射,SeemPigAlikeVolitation.StyleEnumBase的派生类的

循环节点 :foreach (string ClassName in Settings.StyleNames.Split(','))--就是在下面配置的
  <StyleEnum StyleNames="Read,StyleT">
    
<StyleClass>

下面的代码作了2件事:
 MeStyleAll.Add((Style)Enum.Parse(typeof(Style), ClassName), System.Web.Configuration.ProvidersHelper.InstantiateProvider(Settings.StyleClass[ClassName],typeof(StyleEnumBase)) as StyleEnumBase

1:(Style)Enum.Parse(typeof(Style), ClassName)-将字符串转换成枚举

2:利用反射System.Web.Configuration.ProvidersHelper.InstantiateProvider反射设置的基类,
参数:
 Settings.StyleClass[ClassName] 是代表了-节点
typeof(StyleEnumBase)-在把基类的类型传递进去

MM :挺简单的阿:(故意发言不让观众忘记她)

猪:恩接下来是构造,就是调用样式枚举,并且,调用派生类的方法- -面向对象阿

        public StyleEnumBase()
        
{
            asOneStyle.Add(NeedHelp.Color,
this.GetColor);
            asOneStyle.Add(NeedHelp.FontSize,
this.GetFontSize);
           
        }


  public static System.Collections.Generic.List<string> ToStyle(System.Collections.Generic.List<CommandNeed> TempNeed)
   这个方法的左用接受一个     CommandNeed的泛型集合-作为参数



        
/// <summary>
        
/// 得到选择的样式
        
/// </summary>
        
/// <param name="MyDay">输入日期</param>
        
/// <param name="MeStyle">输入选择的样式</param>
        
/// <returns></returns>

        public static System.Collections.Generic.List<string> ToStyle(System.Collections.Generic.List<CommandNeed> TempNeed)
        
{
            System.Collections.Generic.List
<string> ren = new List<string>();
   
//<StyleEnum StyleNames="Read">//Read是表识下面的add name的有多个可以用,号分隔且最后不能出现,
  
//  <StyleClass>
            
//    <add name="Read" type="SeemPigAlikeVolitation.StyleRead" />//这个name在 Style中有对应的枚举阿~~不要搞错了~~最后没有加入错误处理
  
//  </StyleClass>
  
//</StyleEnum>
            
//MeStyleAll.Clear();

            
if (MeStyleAll.Count == 0)
            
{

                Instance();
            }

           
     

            
foreach (CommandNeed onlayNeed in TempNeed)
            
{

                
if (onlayNeed.WhatStyle == null)
                
{
                    
throw new Exception();
                }


      
                        
string WriteStyle = "<span  style=\"";

                        
foreach (NeedHelp onlayNeedHelp in MeStyleAll[onlayNeed.WhatStyle].asOneStyle.Keys)
                        
{
                            
if ((onlayNeed.WhatHelp& onlayNeedHelp) != 0)
                            
{
                                WriteStyle 
+= MeStyleAll[onlayNeed.WhatStyle].asOneStyle[onlayNeedHelp].Replace(";"""+ ";";
                            }

                        }


                        
//--鉴于鄙人订立的规则只能有一个种类样式存在
                       ren.Add(WriteStyle+"\">" + onlayNeed.Info + "</span>");
 
       
            }

            
            
return ren;
        }
天使:上面的代码和第一处循环用的代码是不太一样,主要是给大家写文章的时候突然有点灵感

猪:(怎么好象有人说话
继续:NeedHelp onlayNeedHelp in MeStyleAll[onlayNeed.WhatStyle].asOneStyle.Keys是去除所有的样式参与循环

这里是正终点if(onlayNeed.WhatHelp& onlayNeedHelp)由于用可以从一种样式中挑选自己喜欢的所以需要判断下,当然是按位判断,后面的不用说了吧就是找到样式并且输出

这是调用.....

    // //--经测试这种写的方法不能用于控制台应用程序
        
//SeemPigAlikeVolitation.ConfingPage Settings = System.Configuration.ConfigurationManager.GetSection("StyleEnum") as SeemPigAlikeVolitation.ConfingPage;



        
//SeemPigAlikeVolitation.StyleEnumBase.Instance();
        
        System.Collections.Generic.List
< SeemPigAlikeVolitation.StyleEnumBase.CommandNeed>  temp=new System.Collections.Generic.List<SeemPigAlikeVolitation.StyleEnumBase.CommandNeed>();
        SeemPigAlikeVolitation.StyleEnumBase.CommandNeed cd1 
= new SeemPigAlikeVolitation.StyleEnumBase.CommandNeed();
        cd1.WhatHelp 
= SeemPigAlikeVolitation.NeedHelp.FontSize | SeemPigAlikeVolitation.NeedHelp.Color;
        cd1.WhatStyle 
= SeemPigAlikeVolitation.Style.Read;
        cd1.Info 
= "dd";

        
        temp.Add(cd1);


        SeemPigAlikeVolitation.StyleEnumBase.CommandNeed cd2 
= new SeemPigAlikeVolitation.StyleEnumBase.CommandNeed();
        cd2.WhatHelp 
=  SeemPigAlikeVolitation.NeedHelp.Color;
        cd2.WhatStyle 
= SeemPigAlikeVolitation.Style.StyleT;
        cd2.Info 
= "dd2";


        temp.Add(cd2);



        SeemPigAlikeVolitation.StyleEnumBase.CommandNeed cd3 
= new SeemPigAlikeVolitation.StyleEnumBase.CommandNeed();
        cd3.WhatHelp 
= SeemPigAlikeVolitation.NeedHelp.Color | SeemPigAlikeVolitation.NeedHelp.FontSize;
        cd3.WhatStyle 
= SeemPigAlikeVolitation.Style.StyleT;
        cd3.Info 
= "dd3";


        temp.Add(cd3);
        System.Collections.Generic.List
<string> WriteStyle=SeemPigAlikeVolitation.StyleEnumBase.ToStyle(temp);
        
foreach (string s in WriteStyle)
        
{
            Response.Write(s 
+ "</br>");
        }

就是配置参数集合,然后调用静态方法,最后输出....

由于本人和mm准备去看电影下面~~就到这里吧民田或者后天奉献wwf中文版的使用手记关于电子邮件的部分
posted @ 2007-04-30 22:12  苹果王子  阅读(2064)  评论(3编辑  收藏  举报