马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
前几天同学问我学习方向的问题。有点想法,不知道对不对,怕误导同学,现在“开源一下”。注:括号内是我现在整理的时填加上的。数据 HOWTO:设置或数据文件的保留这个原是基于NET2003,个中又用了2005的泛型(OFTConfigInformation),显得不三不四。如今改成2005的,并作废了接口的引进。
序列化类:
PublicClassSerializeHelperClassSerializeHelper(OfT)
PrivateSubNew()SubNew()
EndSub
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedFunctionItemToXml()FunctionItemToXml(ByValobjAsT)AsString
DimmResultAsString=""
DimmSerializerAsNewSystem.Xml.Serialization.XmlSerializer(GetType(T))
DimmStringWriterAsNewSystem.IO.StringWriter
UsingmStringWriter
mSerializer.Serialize(mStringWriter,obj)
mResult=mStringWriter.ToString
mStringWriter.Close()
EndUsing
ReturnmResult
EndFunction
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedFunctionXmlToItem()FunctionXmlToItem(ByValxmlAsString)AsT
DimmSerializerAsNewSystem.Xml.Serialization.XmlSerializer(GetType(T))
DimmStringReaderAsNewSystem.IO.StringReader(xml)
ReturnCType(mSerializer.Deserialize(mStringReader),T)
EndFunction
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedSubItemToXmlFile()SubItemToXmlFile(ByValfilenameAsString,ByValobjAsT)
DimXmlWriterAsNewSystem.IO.StreamWriter(filename,False)
UsingXmlWriter
XmlWriter.Write(ItemToXml(obj))
XmlWriter.Close()
EndUsing
EndSub
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedFunctionXmlFileToItem()FunctionXmlFileToItem(ByValfilenameAsString)AsT
DimXmlReaderAsNewSystem.IO.StreamReader(filename,System.Text.Encoding.Default)
DimmObjAsT
UsingXmlReader
mObj=XmlToItem(XmlReader.ReadToEnd)
XmlReader.Close()
EndUsing
ReturnmObj
EndFunction
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedSubItemToFormatterFile()SubItemToFormatterFile(ByValfilenameAsString,ByValformatterAsSystem.Runtime.Serialization.IFormatter,ByValobjAsT)
DimmFileStreamAsSystem.IO.Stream=System.IO.File.Open(filename,System.IO.FileMode.Create)
UsingmFileStream
formatter.Serialize(mFileStream,obj)
mFileStream.Close()
EndUsing
EndSub
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedFunctionFormatterFileToItem()FunctionFormatterFileToItem(ByValFileNameAsString,ByValformatterAsSystem.Runtime.Serialization.IFormatter)AsT
DimmFileStreamAsSystem.IO.Stream=System.IO.File.Open(FileName,System.IO.FileMode.Open)
DimmObjAsT
UsingmFileStream
mObj=CType(formatter.Deserialize(mFileStream),T)
mFileStream.Close()
EndUsing
ReturnmObj
EndFunction
PublicSharedFunctionClone()FunctionClone(ByValobjAsT)AsT
DimtmpTAsT
DimmFormatterAsNewSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter
DimmMemoryStreamAsNewSystem.IO.MemoryStream
UsingmMemoryStream
mFormatter.Serialize(mMemoryStream,obj)
mMemoryStream.Position=0
tmpT=CType(mFormatter.Deserialize(mMemoryStream),T)
mMemoryStream.Close()
EndUsing
ReturntmpT
EndFunction
PublicSharedSubSave()SubSave(ByValfilenameAsString,ByValformattypeAsFormatType,ByValobjAsT)
SelectCaseformattype
Caseformattype.Binary
ItemToFormatterFile(filename,NewSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter,obj)
Caseformattype.Soap
ItemToFormatterFile(filename,NewSystem.Runtime.Serialization.Formatters.Soap.SoapFormatter,obj)
Caseformattype.Xml
ItemToXmlFile(filename,obj)
EndSelect
EndSub
PublicSharedFunctionLoad()FunctionLoad(ByValfilenameAsString,ByValformattypeAsFormatType)AsT
SelectCaseformattype
Caseformattype.Binary
ReturnFormatterFileToItem(filename,NewSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter)
Caseformattype.Soap
ReturnFormatterFileToItem(filename,NewSystem.Runtime.Serialization.Formatters.Soap.SoapFormatter)
Caseformattype.Xml
ReturnXmlFileToItem(filename)
EndSelect
ReturnNothing
EndFunction
EndClass
PublicEnumFormatTypeEnumFormatType
Xml
Binary
Soap
EndEnum
设置与数据文件处置类:
PublicMustInheritClassConfigInformationCollectionBaseClassConfigInformationCollectionBase(OfTKey,TValue)
InheritsSystem.Collections.Generic.Dictionary(OfTKey,TValue)
默许为FormatType.Binary
PrivategformattypeAsLzmTW.FormatType=LzmTW.FormatType.Binary
PrivategFileNameAsString=AppDomain.CurrentDomain.BaseDirectory&"{0}.dat"
PublicSubRead()SubRead()
Read(gformattype)
EndSub
PublicSubRead()SubRead(ByValformattypeAsLzmTW.FormatType)
gFileName=String.Format(gFileName,GetType(TValue).Name)
Read(gFileName,formattype)
EndSub
PublicSubRead()SubRead(ByValfileAsString,ByValformattypeAsLzmTW.FormatType)
gFileName=file
gformattype=formattype
Me.Load()
EndSub
PublicShadowsSubAdd()SubAdd(ByValitemAsTValue)
DimmKeyValueAsTKey=Nothing
IfKeyNameIsField()Then
mKeyValue=CType(GetType(TValue).GetField(KeyName).GetValue(item),TKey)
Else
mKeyValue=CType(GetType(TValue).GetProperty(KeyName).GetValue(item,Nothing),TKey)
EndIf
IfNotMe.ContainsKey(mKeyValue)Then
不存在,增添纪录
MyBase.Add(mKeyValue,item)
Else
存在,作修正处置
Me.Item(mKeyValue)=item
EndIf
EndSub
PublicSubSave()SubSave()
DimmItems(Me.Count-1)AsTValue
Me.Values.CopyTo(mItems,0)
SerializeHelper(OfTValue()).Save(gFileName,gformattype,mItems)
EndSub
PrivateSubLoad()SubLoad()
IfNotIO.File.Exists(gFileName)Then
Initialize()
Save()
Else
DimmItems()AsTValue
mItems=CType(SerializeHelper(OfTValue()).Load(gFileName,gformattype),TValue())
ForEachitemAsTValueInmItems
Me.Add(item)
Next
EndIf
EndSub
<summary>
承继时,如有初始赋值,在此完成
</summary>
ProtectedMustOverrideSubInitialize()SubInitialize()
<summary>
指定TValue的键名(字段或属性)的称号
</summary>
ProtectedMustOverrideReadOnlyPropertyKeyName()PropertyKeyName()AsString
<summary>
指定TValue的键名是字段(真)仍是属性(假)
</summary>
ProtectedMustOverrideReadOnlyPropertyKeyNameIsField()PropertyKeyNameIsField()AsBoolean
SubNew()SubNew()
检测键名是不是无效,键名的类和TKey是不是符合
IfKeyNameIsField()Then
DimmFieldInfoAsReflection.FieldInfo=GetType(TValue).GetField(KeyName)
IfmFieldInfoIsNothingThen
ThrowNewSystemException(String.Format("类{0}中不存在称号为{1}的字段",GetType(TValue).Name,KeyName))
Else
IfNotmFieldInfo.FieldType.Name.Equals(GetType(TKey).Name)Then
ThrowNewSystemException(String.Format("类{0}中字段称号为{1}的范例为{2},与指定的键值范例{3}不符",GetType(TValue).Name,KeyName,mFieldInfo.FieldType.Name,GetType(TKey).Name))
EndIf
EndIf
Else
DimmPropertyInfoAsReflection.PropertyInfo=GetType(TValue).GetProperty(KeyName)
IfmPropertyInfoIsNothingThen
ThrowNewSystemException(String.Format("类{0}中不存在称号为{1}的属性",GetType(TValue).Name,KeyName))
Else
IfNotmPropertyInfo.PropertyType.Name.Equals(GetType(TKey).Name)Then
ThrowNewSystemException(String.Format("类{0}中属性称号为{1}的范例为{2},与指定的键值范例{3}不符",GetType(TValue).Name,KeyName,mPropertyInfo.PropertyType.Name,GetType(TKey).Name))
EndIf
EndIf
EndIf
EndSub
EndClass
使用以下:
1、界说设置或数据类
<Serializable()>_
PublicClassMyConfigInfoClassMyConfigInfo
PrivatemMachineAsString
PrivatemLogins(-1)AsLogin
PublicPropertyMachine()PropertyMachine()AsString
Get
ReturnmMachine
EndGet
Set(ByValvalueAsString)
mMachine=value
EndSet
EndProperty
PublicReadOnlyPropertyLogins()PropertyLogins()AsLogin()
Get
ReturnmLogins
EndGet
EndProperty
PublicSubAdd()SubAdd(ByValloginAsLogin)
ReDimPreservemLogins(mLogins.Length)
mLogins(mLogins.Length-1)=login
EndSub
<Serializable()>_
PublicClassLoginClassLogin
PrivatemUserAsString
PrivatemPassAsString
PublicPropertyUser()PropertyUser()AsString
Get
ReturnmUser
EndGet
Set(ByValvalueAsString)
mUser=value
EndSet
EndProperty
PublicPropertyPass()PropertyPass()AsString
Get
ReturnmPass
EndGet
Set(ByValvalueAsString)
mPass=value
EndSet
EndProperty
SubNew()SubNew()
EndSub
SubNew()SubNew(ByValuserAsString,ByValpassAsString)
Me.User=user
Me.Pass=pass
EndSub
EndClass
EndClass
2、完成处置
PublicClassConfigDataClassConfigData
InheritsConfigInformationCollectionBase(OfString,MyConfigInfo)
ProtectedOverridesSubInitialize()SubInitialize()
EndSub
ProtectedOverridesReadOnlyPropertyKeyName()PropertyKeyName()AsString
Get
Return"Machine"
EndGet
EndProperty
ProtectedOverridesReadOnlyPropertyKeyNameIsField()PropertyKeyNameIsField()AsBoolean
Get
ReturnFalse
EndGet
EndProperty
EndClass
测试:
PrivateSubButton3_Click()SubButton3_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton3.Click
DimtestAsNewConfigData
读文件
test.Read()
DimitemAsMyConfigInfo
item=NewMyConfigInfo
Withitem
.Machine="Fk-A01-02"
.Add(NewMyConfigInfo.Login("LzmTW","001"))
.Add(NewMyConfigInfo.Login("Lzm","002"))
EndWith
test.Add(item)
item=NewMyConfigInfo
Withitem
.Machine="Fk-A01-03"
.Add(NewMyConfigInfo.Login("L","003"))
.Add(NewMyConfigInfo.Login("Lz","004"))
.Add(NewMyConfigInfo.Login("LzmTW","001"))
EndWith
test.Add(item)
Console.WriteLine(test.Item("Fk-A01-03").Logins(0).User)
test.Item("Fk-A01-03").Logins(0).User="Hello"
test.Save()存盘
用另外一个翻开,看看
Dimtest2AsNewConfigData
test2.Read()
Console.WriteLine(test2.Item("Fk-A01-03").Logins(0).User)
EndSub
捆绑编译器。用户不需要受制于厂家,自己就能将程序在新平台上编译运行。除了牛B轰轰的linux,估计也没有系统捆绑c/c++的编译器,而且许多新平台都无法支持复杂的c/c++编译器在上面直接运行。 |