VB.NET System.ComponentModel.Component.Site 标记无法序列化,因为它是一个接口


Imports System.IO.Ports
Imports System.Data.SqlClient
Imports System.Collections.ObjectModel
Imports System.Data.Common
Imports Microsoft
Imports System.Data.SqlTypes
Imports System.Xml
Imports System.Collections.Specialized.BitVector32
Imports System.Security.Cryptography.X509Certificates
Imports System.Xml.Serialization
Imports System.Windows.Forms.AxHost
Public Class sqlConnector
'Private Session
Private pSqlServer, pSqlUsername, pSqlPassword, pSqlDatabase, pSqlTable As String
Private pSqlConnection As New SqlConnection
Private pSqlCmd As SqlCommand
Private pSqlReader As SqlDataReader
'Public Session
Public Sub New()
End Sub
Public Property serverName As String
Get
Return pSqlServer
End Get
Set(value As String)
pSqlServer = value
End Set
End Property
Public Property userName As String
Get
Return pSqlUsername
End Get
Set(value As String)
pSqlUsername = value
End Set
End Property
Public Property dataBaseName As String
Get
Return pSqlDatabase
End Get
Set(value As String)
pSqlDatabase = value
End Set
End Property
Public Property password As String
Get
Return pSqlPassword
End Get
Set(value As String)
pSqlPassword = value
End Set
End Property
Public Property table As String
Get
Return pSqlTable
End Get
Set(value As String)
pSqlTable = value
End Set
End Property
Public Function connect()
Try
pSqlConnection = New SqlConnection("Server=" & pSqlServer & ";User Id=" & pSqlUsername & ";Password=" & pSqlPassword)
pSqlConnection.Open()
Return 0
Catch ex As Exception
MsgBox("Error: " & ex.Message, vbCritical + vbOKOnly)
Return -1
End Try
End Function
Public Function send(command As String)
If pSqlConnection.State <> ConnectionState.Open Then
If connect() <> 0 Then Exit Function
End If
Try
pSqlCmd = pSqlConnection.CreateCommand
pSqlCmd.CommandText = command
pSqlReader = pSqlCmd.ExecuteReader()
If pSqlReader.HasRows Then
Return pSqlReader
Else
Return 0
End If
Catch ex As Exception
MsgBox("Error: " & ex.Message, vbCritical + vbOKOnly)
Return -1
End Try
End Function
Public Sub disConnect()
pSqlConnection.Close()
End Sub
Protected Overrides Sub Finalize()
disConnect()
End Sub
End Class
Public Class Scale
'Private Session
Public pScaleName As String
Private pScaleId As Integer
Private pSerialConnection As SerialPort
Private pSqlConnection As sqlConnector
Private Sub SendToSQL(data As String)
Dim splittedData() As String = data.Split(" ")
pSqlConnection.send("USE" & pSqlConnection.dataBaseName & "; UPDATE " & pSqlConnection.table & " set value=" & splittedData(0) & ", unit=" & splittedData(1) & " where deviceid=" & pScaleId)
End Sub
Private Sub DataReceivedHandler(sender As Object, e As SerialDataReceivedEventArgs)
Dim buffered As String = ""
Try
Dim rcv As String = pSerialConnection.ReadLine()
buffered = rcv.Trim()
If buffered <> "" Then
SendToSQL(buffered)
End If
Catch ex As Exception
End Try
End Sub
Private Sub serialDisConnect()
pSerialConnection.Close()
End Sub
'Public Session
Public Sub New()
End Sub
Public Property sqlConnection As sqlConnector
Get
Return pSqlConnection
End Get
Set(value As sqlConnector)
pSqlConnection = value
End Set
End Property
Public Property serialConnection As SerialPort
Get
Return pSerialConnection
End Get
Set(value As SerialPort)
pSerialConnection = value
End Set
End Property
Public Property name As String
Get
Return pScaleName
End Get
Set(value As String)
pScaleName = value
End Set
End Property
Public Property id As Integer
Get
Return pScaleId
End Get
Set(value As Integer)
pScaleId = value
End Set
End Property
Public Function serialConnect()
Try
pSerialConnection.Open()
AddHandler pSerialConnection.DataReceived, AddressOf DataReceivedHandler
Return 0
Catch ex As Exception
MsgBox("Error opening port " & CStr(pSerialConnection.PortName) & "! Error: " & ex.Message, "Error opening port", vbCritical + vbOKOnly)
Return -1
End Try
End Function
Protected Overrides Sub Finalize()
serialDisConnect()
End Sub
End Class
Public Class Settings
Private pFileName As String
Private pSections As New List(Of Section)
<System.Xml.Serialization.XmlInclude(GetType(Scale))>
<System.Xml.Serialization.XmlInclude(GetType(sqlConnector))>
Public Class Section
Private pName As String
Private pContent As New List(Of Object)
Public Property name As String
Get
Return pName
End Get
Set(value As String)
pName = value
End Set
End Property
Public ReadOnly Property content(item As String) As Object
Get
For i As Integer = 0 To pContent.Count() - 1
If pContent(i).name = item Then Return pContent(i)
Next
Return Nothing
End Get
End Property
Public Sub addContent(item)
pContent.Add(item)
End Sub
Public Sub New()
End Sub
End Class
Public Sub New()
End Sub
Public Sub saveSettings()
If pFileName = Nothing Then
pFileName = Application.StartupPath & "settings.cfg"
End If
Dim tXML As New System.Xml.Serialization.XmlSerializer(GetType(List(Of Section)))
Dim tSourceFile As New System.IO.StreamWriter(pFileName)
tXML.Serialize(tSourceFile, pSections)
tSourceFile.Close()
End Sub
Public Sub loadSettings()
End Sub
Public ReadOnly Property sections(item As String) As Section
Get
For i As Integer = 0 To pSections.Count() - 1
If pSections(i).name = item Then Return pSections(i)
Next
Return Nothing
End Get
End Property
Public Sub addSection(name As String)
Dim tempSection As New Section
tempSection.name = name
pSections.Add(tempSection)
End Sub
End Class
Module modMain
'Variables
Public scales As New List(Of Scale)
Public settings As New Settings
Public Sub addScale(scaleName As String, scaleId As Integer, ByRef sqlConnection As sqlConnector, ByRef serialConnector As SerialPort, ByRef scales As List(Of Scale))
Dim scale As New Scale
scale.name = scaleName
scale.id = scaleId
scale.sqlConnection = sqlConnection
scale.serialConnection = serialConnector
scales.Add(scale)
End Sub
Public Sub main()
Dim sql01 As New sqlConnector
Dim serial01 As New SerialPort
addScale("scale01", 178, sql01, serial01, scales)
settings.addSection("merlegek")
settings.addSection("sqlkapcsolatok")
settings.sections("merlegek").addContent(scales(0))
settings.sections("sqlkapcsolatok").addContent(sql01)
settings.SaveSettings()
End Sub
End Module

这是我的密码。

在运行时,它以System.InvalidOperationException停止:System.ComponentModel.Component.Site标记无法序列化,因为它是一个接口。该程序基本上应该控制串行端口上连接的几个天平,并将读取的数据发送到sql数据库。我创建了数据结构。我想把它保存到一个XML文件中(以便下次加载(。我试图找到任何关于堆栈溢出问题的建议,但没有找到任何相关的。。。

作为Craig

由于SerialPort继承自System.ComponentModel.Component,因此将在尝试序列化serialConnection时发出此消息。我认为您必须将该属性/字段标记为不可序列化,并序列化足够的辅助信息,以便在反序列化时重新建立端口对象。

在我用<System.Xml.Serialization.XmlIgnore>,并且当我将pContent公开时,序列化工作开始了。

相关内容

最新更新