⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 choicelist.cs

📁 本在线考试系统采用了面向对象的分析和设计
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.UserTypes;
using NHibernate.SqlTypes;
using System.Data;
using System.Xml;
using ExaminationSystem.BLL.Domain;

namespace ExaminationSystem.DAL.UserType
{
    public class ChoiceList : IUserType
    {

        private static SqlType[] TYPES = new SqlType[] { new SqlType(DbType.String) };

        public bool IsMutable
        {
            get { return false; }
        }
        public Type ReturnedType
        {
            get { return typeof(IList<Choice>); }
        }

        public SqlType[] SqlTypes
        {
            get
            {
                return TYPES;
            }
        }

        public object DeepCopy(object value)
        {
            IList<Choice> sourceList = (IList<Choice>)value;
            IList<Choice> targetList = new List<Choice>();
            foreach (Choice choice in sourceList)
                targetList.Add(choice);
            return targetList;
        }

        public new bool Equals(object x, object y)
        {
            if (x == y) return true;
            if (x != null && y != null)
            {
                IList<Choice> listX = (IList<Choice>)x;
                IList<Choice> listY = (IList<Choice>)y;
                if (listX.Count != listY.Count) return false;
                for (int i = 0; i < listX.Count; i++)
                {
                    if (listX[i] != listY[i]) return false;
                }
                return true;
            }
            return true;
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }

        public object Assemble(object cached, object owner)
        {
            return DeepCopy(cached);
        }

        public object Disassemble(object value)
        {
            return DeepCopy(value);
        }

        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            string value = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
            if (value == null)
                return null;
            else
                return ParseChoices(value);
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (value != null)
            {
                string str = AssembleChoices((IList<Choice>)value);
                NHibernateUtil.String.NullSafeSet(cmd, str, index);
            }
            else
                NHibernateUtil.String.NullSafeSet(cmd, value, index);
        }

        public object Replace(object original, object target, object owner)
        {
            return original;
        }

        private IList<Choice> ParseChoices(string value)
        {
            XmlTextReader tr = new XmlTextReader(value, XmlNodeType.Element, null);
            IList<Choice> choiceList = new List<Choice>();
            while (tr.Read())
            {
                if (tr.NodeType == XmlNodeType.Element)
                {
                    Choice item = new Choice();
                    item.Index = int.Parse(tr.GetAttribute(0));

                    tr.Read();

                    item.Value = tr.Value;

                    choiceList.Add(item);
                }
            }


            return choiceList;
        }
        private string AssembleChoices(IList<Choice> choices)
        {
            string str = "";
            foreach (Choice choice in choices)
                str += choice.GetXML();
            return str;
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -