我女友给我发了一个文章的地址,让我帮着她把程序作出来。我也自信满满的,一作,晕了,比侦探小说还费劲,一想自己也就作asp.net一年多,肯定实力不够,特来CSDN来,请前辈,高人,强人来帮一把。谢谢了,解决后一定给分。http://guoshiguan.javaeye.com/blog/78196就是上面说的文章,我想倒推出那样的程序,是验证的,估计作者是封在一个DLL中,以便日后重用的。还是一句话,高人,强人,出手相助啊,关乎小弟的面子,女朋友一直拿我当强人呢(不好意思了。)另一个百分贴子在:
http://community.csdn.net/Expert/topic/5715/5715403.xml?temp=.4403498如果您解决了我的问题,请在那个帖子上留名,我会即时给分的。

解决方案 »

  1.   

    我懂了,就是把那个帖子的程序作来是不LZ,我明白是明白,但是初学,作不出,JF
      

  2.   

    不懂 ilosql() ( ) 信誉:100  2007-08-17 16:01:05  得分: 0  
    你真的懂了??
      

  3.   

    to andy888666
    您要是看到我说的那篇文章,就会发现,它缺少代码,所以,我现此求高人,倒着推出来,本人对特性,也只是知道而已,试试两天也没有作出来,可是又要到周日了,GF,要我完成,真是急啊。
      

  4.   

    guoweidong(※『孤独~寂)您细看一下,就会发现,它有几个特性没有给出来,不信您自己试着封装一次,编译器就会给您提错的。
      

  5.   

    那篇文章中,没有提供Validator.Configuration.ValidateConfigurationSection类ValidateType类[ValidateType("NotNull")] 
    [TestObj] 
    [StringLengthValidateType(2)] 这三亿特性。
      

  6.   

    缺少  ValidateType  类
      

  7.   

    xuyiazl您看出来了,好,其实它还缺Validator.Configuration.ValidateConfigurationSection类
      

  8.   

    using System;
    using System.Configuration;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;namespace Validator.Configuration
    {
        public interface IValidateType
        {
            string Param { get;set;}
            string ValidatorName { get;set;}
            string ColName { get;set;}
        }    public interface IValidator
        {
            bool validator(object value, ValidatorInfo vi, IValidateType vt);
        }    public enum validateType
        {
            inner,
            match,
            clazz
        }
        public class ValidatorInfo
        {
            private string info;
            private string msg;        public ValidatorInfo(string info, string msg)
            {
                this.info = info;
                this.msg = msg;
            }
        }
        public class ValidateType : Attribute, IValidateType
        {
            protected string param;
            protected string validatorName;
            protected string colName;        public ValidateType()
            {
            }        public ValidateType(string name)
            {
                validatorName = name;
            }        #region IValidateType Members        public string Param
            {
                get { return param; }
                set { param = value; }
            }        public string ValidatorName
            {
                get { return validatorName; }
                set { validatorName = value; }
            }        public string ColName
            {
                get { return colName; }
                set { colName = value; }
            }        #endregion
        }
        public class ValidateConfigurationSection : IConfigurationSectionHandler
        {
            Dictionary<string, IValidateType> validateTypes = new Dictionary<string, IValidateType>();        #region IConfigurationSectionHandler Members        public object Create(object parent, object configContext, System.Xml.XmlNode section)
            {
                foreach (XmlNode node in section.ChildNodes)
                {
                    XmlElement el = node as XmlElement;
                    if (el == null)
                    {
                        continue;
                    }                string type = el.GetAttribute("validateType");
                    string name = el.GetAttribute("name");
                    string info = el.GetAttribute("validateInfo");
                    string msg = el.GetAttribute("message");                ValidatorInfo vi = new ValidatorInfo(info, msg);
                    IValidateType vt = null;                switch (type)
                    {
                        case "inner":
                            vt = CreateInnerValidateType(name, vi);
                            break;                    case "match":
                            // create regex validate type here
                            break;                    case "clazz":
                            // create class validate type here by reflacting
                            break;                    default:
                            vt = new ValidateType();
                            break;
                    }                validateTypes.Add(name, vt);
                }            return validateTypes;
            }        #endregion
            private ValidateType CreateInnerValidateType(string name, ValidatorInfo vi)
            {
                // create inner validate type here by checking name
                // swith or reflect
                return null;
            }
        }
        public class NotNullValidateType : ValidateType
        {
            public NotNullValidateType(String sColName)
            {
                this.ColName = sColName;
                this.ValidatorName = "NotNull";
            }
        }
        public class ValidateUtile : IValidator
        {
            #region IValidator Members        public bool validator(object value, ValidatorInfo vi, IValidateType vt)
            {
                throw new Exception("The method or operation is not implemented.");
            }        #endregion
        }
        public class StringLengthValidateType : ValidateType
        {
            protected int maxLength;        public StringLengthValidateType(int maxLen)
            {
                maxLength = maxLen;
                validatorName = "StringLength";
            }
        }
        public class EmailValidateType : ValidateType
        {
            public EmailValidateType(string sColName)
            {
                colName = sColName;
                validatorName = "Email";
            }
        }
        public class DecimalValidateType : ValidateType
        {
            protected int a;
            protected int b;        public DecimalValidateType(int n, int m)
            {
                a = n;
                b = m;
                validatorName = "Decima";
            }
        }}
      

  9.   

    using System;
    using System.Collections.Generic;
    using System.Text;using Validator.Configuration;namespace Validator.Test
    {
        public class TestObj : ValidateType, IValidator
        {
            public TestObj()
            {
                this.ValidatorName = "TestClass";
            }        public TestObj(String sColName)
            {
                this.ColName = sColName;
                this.ValidatorName = "TestClass";
            }        #region IValidator 成员        public bool validator(object value, ValidatorInfo vi, IValidateType vt)
            {
                //Console.Write("hello world gettype"); 
                return false;
            }
            #endregion
        }}
      

  10.   

    using System;
    using System.Collections.Generic;
    using System.Text;using Validator.Configuration;
    using Validator.Test;namespace ConsoleApplication1
    {
        class Program
        {
            public class UserObject
            {
                string _code;
                string _email;            [ValidateType("NotNull")]
                [TestObj]
                [StringLengthValidateType(2)]
                public string Code
                {
                    get { return _code; }
                    set { _code = value; }
                }            [EmailValidateType(null)]
                public string Email
                {
                    get { return _email; }
                    set { _email = value; }
                }            decimal _age;
                [DecimalValidateType(0, 50)]
                public decimal Age
                {
                    get { return _age; }
                    set { _age = value; }
                }        }        static void Main(string[] args)
            {
                ValidateUtile vu = new ValidateUtile();
                UserObject uo = new UserObject();
            }
        }
    }
      

  11.   

    很清楚啊,没啥不清楚的<configSections>   
            <sectionGroup name="Validate">   
                <section name="Validators" type="Validator.Configuration.ValidateConfigurationSection, Validator"/>   
            </sectionGroup>  程序会读取这个配置 ,利用反射调用Validator.Configuration.ValidateConfigurationSection, Validatorname字段表明需要在程序中读取web.configpei配置节的那一段,这里是读取Validate节
    即下面的节
    <Validate>   
            <Validators>   
          <Validator name="Email" validateType="match" validateInfo="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" message="{0} is\'t Email"/>   
          <Validator name="NotNull" validateType="inner" validateInfo="NotNull" message="{0}不能为空!"/>   
          <Validator name="Length" validateType="inner" validateInfo="Length" message="{0}字符串长度不能超过{1}!"/>   
          <Validator name="Decimal" validateType="inner" validateInfo="Decimal" message="{0}必须大于{1},{0}必须小于{1}"/>   
          <Validator name="TestClass" validateType="clazz" validateInfo="Validator.Test.TestObj, Validator" message="{0}class测试!"/>   
            </Validators>   
        </Validate>  
    根据这个配置会,把这些东西放入一个枚举中程序调用,会用一个工厂类来决定具体取枚举中的那个类剩下的也就是正则判断,RegisterStartupScript脚本的事情了
      

  12.   

    renix(renix)您是不是就是那篇文章的作者,如果不是,你推理的思路也太清晰了,简直神了,又在CSDN查了一下,你在下载频道有  “改善.net程序的性能和可测性 ”,这功力明显不是一个三角能达到的。先谢谢您让我免了皮肉之苦,请问给我回个信,我想认识您。
      

  13.   

    您是不是就是那篇文章的作者,如果不是,你推理的思路也太清晰了,简直神了,又在CSDN查了一下,你在下载频道有  “改善.net程序的性能和可测性 ”,这功力明显不是一个三角能达到的。
    =======================
    哈,有些N人可能不热衷于赚分..
      

  14.   

    我不是原作者,只是倒推了一下。不过代码不是完整的。而且我觉得原作有些问题。原作的ValidateType 和Validator概念用的比较混乱。看下IValidator 接口:
    public interface IValidator
        {
            bool validator(object value, ValidatorInfo vi, IValidateType vt);
        }
    按照用法来看,这个value是要被验证的类。但是这个类中已经有了matadata来表示的使用的验证器的信息,也就是UserObject中property的attribute(中文都叫属性)。这个信息可以在validator函数中使用反射来得出。那么,为什么这个函数还要传个vt呢?又有2个validateType,一个是枚举,一个是类。混乱。xml中的配置也混乱。TestObj是一个validator,而他的配置方法却和其他的ValidateType一样,明显是后来凑上去的。
      

  15.   

    [ValidateType("NotNull")]
    [TestObj]
    [StringLengthValidateType(2)]
    public string Code
    {
    get { return _code; }
    set { _code = value; }
    }[EmailValidateType(null)]
    public string Email
    {
    get { return _email; }
    set { _email = value; }
    }decimal _age;
    [DecimalValidateType(0, 50)]
    public decimal Age
    {
    get { return _age; }
    set { _age = value; }
    }这些[]都是属性吗,本原属性不是应该继承自Attributes类吗,可是代码中从来也没有出现过啊??LZ,借地问一下,请大家给我说一说。
      

  16.   

    renix   真挺强的,比我强。to ilosql() public class ValidateType : Attribute, IValidateType
        {                        ↑这是什么^_^