国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

HOW TO:配置或數(shù)據(jù)文件的保存(改進(jìn))

2024-07-21 02:29:17
字體:
供稿:網(wǎng)友
  • 網(wǎng)站運(yùn)營seo文章大全
  • 提供全面的站長運(yùn)營經(jīng)驗(yàn)及seo技術(shù)!
  •   how to:配置或數(shù)據(jù)文件的保存 這個原是基于net2003,其中又用了2005的泛型(of tconfiginformation),顯得不倫不類。現(xiàn)在改為2005的,并取消了接口的引入。

    序列化類:

    public class serializehelperclass serializehelper(of t)

        private sub new()sub new()
        end sub

        <system.componentmodel.editorbrowsable(componentmodel.editorbrowsablestate.advanced)> _
        public shared function itemtoxml()function itemtoxml(byval obj as t) as string
            dim mresult as string = ""
            dim mserializer as new system.xml.serialization.xmlserializer(gettype(t))
            dim mstringwriter as new system.io.stringwriter
            using mstringwriter
                mserializer.serialize(mstringwriter, obj)
                mresult = mstringwriter.tostring
                mstringwriter.close()
            end using
            return mresult
        end function

        <system.componentmodel.editorbrowsable(componentmodel.editorbrowsablestate.advanced)> _
        public shared function xmltoitem()function xmltoitem(byval xml as string) as t
            dim mserializer as new system.xml.serialization.xmlserializer(gettype(t))
            dim mstringreader as new system.io.stringreader(xml)
            return ctype(mserializer.deserialize(mstringreader), t)
        end function

        <system.componentmodel.editorbrowsable(componentmodel.editorbrowsablestate.advanced)> _
        public shared sub itemtoxmlfile()sub itemtoxmlfile(byval filename as string, byval obj as t)
            dim xmlwriter as new system.io.streamwriter(filename, false)
            using xmlwriter
                xmlwriter.write(itemtoxml(obj))
                xmlwriter.close()
            end using
        end sub

        <system.componentmodel.editorbrowsable(componentmodel.editorbrowsablestate.advanced)> _
        public shared function xmlfiletoitem()function xmlfiletoitem(byval filename as string) as t
            dim xmlreader as new system.io.streamreader(filename, system.text.encoding.default)
            dim mobj as t
            using xmlreader
                mobj = xmltoitem(xmlreader.readtoend)
                xmlreader.close()
            end using
            return mobj
        end function

        <system.componentmodel.editorbrowsable(componentmodel.editorbrowsablestate.advanced)> _
        public shared sub itemtoformatterfile()sub itemtoformatterfile(byval filename as string, byval formatter as system.runtime.serialization.iformatter, byval obj as t)
            dim mfilestream as system.io.stream = system.io.file.open(filename, system.io.filemode.create)
            using mfilestream
                formatter.serialize(mfilestream, obj)
                mfilestream.close()
            end using
        end sub

        <system.componentmodel.editorbrowsable(componentmodel.editorbrowsablestate.advanced)> _
        public shared function formatterfiletoitem()function formatterfiletoitem(byval filename as string, byval formatter as system.runtime.serialization.iformatter) as t
            dim mfilestream as system.io.stream = system.io.file.open(filename, system.io.filemode.open)
            dim mobj as t
            using mfilestream
                mobj = ctype(formatter.deserialize(mfilestream), t)
                mfilestream.close()
            end using
            return mobj
        end function

        public shared function clone()function clone(byval obj as t) as t
            dim tmpt as t
            dim mformatter as new system.runtime.serialization.formatters.binary.binaryformatter
            dim mmemorystream as new system.io.memorystream
            using mmemorystream
                mformatter.serialize(mmemorystream, obj)
                mmemorystream.position = 0
                tmpt = ctype(mformatter.deserialize(mmemorystream), t)
                mmemorystream.close()
            end using
            return tmpt
        end function

        public shared sub save()sub save(byval filename as string, byval formattype as formattype, byval obj as t)
            select case formattype
                case formattype.binary
                    itemtoformatterfile(filename, new system.runtime.serialization.formatters.binary.binaryformatter, obj)
                case formattype.soap
                    itemtoformatterfile(filename, new system.runtime.serialization.formatters.soap.soapformatter, obj)
                case formattype.xml
                    itemtoxmlfile(filename, obj)
            end select
        end sub

        public shared function load()function load(byval filename as string, byval formattype as formattype) as t
            select case formattype
                case formattype.binary
                    return formatterfiletoitem(filename, new system.runtime.serialization.formatters.binary.binaryformatter)
                case formattype.soap
                    return formatterfiletoitem(filename, new system.runtime.serialization.formatters.soap.soapformatter)
                case formattype.xml
                    return xmlfiletoitem(filename)
            end select
            return nothing
        end function

    end class

    public enum formattypeenum formattype
        xml
        binary
        soap
    end enum

    配置與數(shù)據(jù)文件處理類:

        public mustinherit class configinformationcollectionbaseclass configinformationcollectionbase(of tkey, tvalue)
            inherits system.collections.generic.dictionary(of tkey, tvalue)

            '默認(rèn)為formattype.binary
            private gformattype as lzmtw.formattype = lzmtw.formattype.binary
            private gfilename as string = appdomain.currentdomain.basedirectory & "{0}.dat"

            public sub read()sub read()
                read(gformattype)
            end sub

            public sub read()sub read(byval formattype as lzmtw.formattype)
                gfilename = string.format(gfilename, gettype(tvalue).name)
                read(gfilename, formattype)
            end sub

            public sub read()sub read(byval file as string, byval formattype as lzmtw.formattype)
                gfilename = file
                gformattype = formattype
                me.load()
            end sub

            public shadows sub add()sub add(byval item as tvalue)
                dim mkeyvalue as tkey = nothing
                if keynameisfield() then
                    mkeyvalue = ctype(gettype(tvalue).getfield(keyname).getvalue(item), tkey)
                else
                    mkeyvalue = ctype(gettype(tvalue).getproperty(keyname).getvalue(item, nothing), tkey)
                end if

                if not me.containskey(mkeyvalue) then
                    '不存在,增加記錄
                    mybase.add(mkeyvalue, item)
                else
                    '存在,作修改處理
                    me.item(mkeyvalue) = item
                end if
            end sub

            public sub save()sub save()
                dim mitems(me.count - 1) as tvalue
                me.values.copyto(mitems, 0)
                serializehelper(of tvalue()).save(gfilename, gformattype, mitems)
            end sub

            private sub load()sub load()
                if not io.file.exists(gfilename) then
                    initialize()
                    save()
                else
                    dim mitems() as tvalue
                    mitems = ctype(serializehelper(of tvalue()).load(gfilename, gformattype), tvalue())
                    for each item as tvalue in mitems
                        me.add(item)
                    next
                end if
            end sub

            ''' <summary>
            ''' '繼承時,若有初始賦值,在此實(shí)現(xiàn)
            ''' </summary>
            protected mustoverride sub initialize()sub initialize()

            ''' <summary>
            ''' 指定tvalue的鍵名(字段或?qū)傩?的名稱
            ''' </summary>
            protected mustoverride readonly property keyname()property keyname() as string

            ''' <summary>
            ''' 指定tvalue的鍵名是字段(真)還是屬性(假)
            ''' </summary>
            protected mustoverride readonly property keynameisfield()property keynameisfield() as boolean

            sub new()sub new()
                '檢測鍵名是否有效,鍵名的類和tkey是否相符
                if keynameisfield() then
                    dim mfieldinfo as reflection.fieldinfo = gettype(tvalue).getfield(keyname)
                    if mfieldinfo is nothing then
                        throw new systemexception(string.format("類{0}中不存在名稱為{1}的字段", gettype(tvalue).name, keyname))
                    else
                        if not mfieldinfo.fieldtype.name.equals(gettype(tkey).name) then
                            throw new systemexception(string.format("類{0}中字段名稱為{1}的類型為{2},與指定的鍵值類型{3}不符", gettype(tvalue).name, keyname, mfieldinfo.fieldtype.name, gettype(tkey).name))
                        end if
                    end if
                else
                    dim mpropertyinfo as reflection.propertyinfo = gettype(tvalue).getproperty(keyname)
                    if mpropertyinfo is nothing then
                        throw new systemexception(string.format("類{0}中不存在名稱為{1}的屬性", gettype(tvalue).name, keyname))
                    else
                        if not mpropertyinfo.propertytype.name.equals(gettype(tkey).name) then
                            throw new systemexception(string.format("類{0}中屬性名稱為{1}的類型為{2},與指定的鍵值類型{3}不符", gettype(tvalue).name, keyname, mpropertyinfo.propertytype.name, gettype(tkey).name))
                        end if
                    end if
                end if

            end sub
        end class

    應(yīng)用如下:
    1、定義配置或數(shù)據(jù)類

    <serializable()> _
    public class myconfiginfoclass myconfiginfo
        private mmachine as string
        private mlogins(-1) as login

        public property machine()property machine() as string
            get
                return mmachine
            end get
            set(byval value as string)
                mmachine = value
            end set
        end property

        public readonly property logins()property logins() as login()
            get
                return mlogins
            end get
        end property

        public sub add()sub add(byval login as login)
            redim preserve mlogins(mlogins.length)
            mlogins(mlogins.length - 1) = login
        end sub

        <serializable()> _
        public class loginclass login
            private muser as string
            private mpass as string

            public property user()property user() as string
                get
                    return muser
                end get
                set(byval value as string)
                    muser = value
                end set
            end property

            public property pass()property pass() as string
                get
                    return mpass
                end get
                set(byval value as string)
                    mpass = value
                end set
            end property

            sub new()sub new()
            end sub

            sub new()sub new(byval user as string, byval pass as string)
                me.user = user
                me.pass = pass
            end sub
        end class
    end class

      2、實(shí)現(xiàn)處理

    public class configdataclass configdata
        inherits configinformationcollectionbase(of string, myconfiginfo)

        protected overrides sub initialize()sub initialize()
        end sub

        protected overrides readonly property keyname()property keyname() as string
            get
                return "machine"
            end get
        end property

        protected overrides readonly property keynameisfield()property keynameisfield() as boolean
            get
                return false
            end get
        end property

    end class

      測試:

        private sub button3_click()sub button3_click(byval sender as system.object, byval e as system.eventargs) handles button3.click
            dim test as new configdata
            '讀文件
            test.read()
            dim item as myconfiginfo

            item = new myconfiginfo
            with item
                .machine = "fk-a01-02"
                .add(new myconfiginfo.login("lzmtw", "001"))
                .add(new myconfiginfo.login("lzm", "002"))
            end with
            test.add(item)

            item = new myconfiginfo
            with item
                .machine = "fk-a01-03"
                .add(new myconfiginfo.login("l", "003"))
                .add(new myconfiginfo.login("lz", "004"))
                .add(new myconfiginfo.login("lzmtw", "001"))
            end with
            test.add(item)
            console.writeline(test.item("fk-a01-03").logins(0).user)
            test.item("fk-a01-03").logins(0).user = "hello"

            test.save() '存盤
            '用另一個打開,看看
            dim test2 as new configdata
            test2.read()
            console.writeline(test2.item("fk-a01-03").logins(0).user)
        end sub

    發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗(yàn)證碼: 匿名發(fā)表
    主站蜘蛛池模板: 武乡县| 南漳县| 林州市| 韩城市| 金溪县| 区。| 松滋市| 卓尼县| 台安县| 贺兰县| 佛坪县| 延安市| 呼和浩特市| 喀什市| 西畴县| 荣昌县| 乌恰县| 东乡县| 朝阳县| 开阳县| 揭东县| 简阳市| 思茅市| 修武县| 东乡族自治县| 札达县| 石台县| 清远市| 留坝县| 玛纳斯县| 镇赉县| 满洲里市| 贵州省| 扬州市| 巴林右旗| 夏河县| 永吉县| 湘阴县| 墨竹工卡县| 漠河县| 紫金县|