我在这里很新(我知道这个网站已经很久了,但这是我第一次真正询问某些内容)。
我正在使用的组件: - EF6,Devexpress XtraGrid
好的......所以,我想要的是这样做, 我有一个包含多个表的表单,我必须能够从每个NavigationBar中添加和删除。
我知道怎么做,我只需要一种跳过选择案例的方法。
这是一些代码,
Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
Dim bList As BindingList(Of Object) = TryCast(sender, BindingList(Of Object))
Dim m As Object = bList.LastOrDefault()
If m IsNot Nothing Then
Select Case _curentPageIndex
Case 0 : db.GESTARM.Add(m)
Case 1 : 'Other table add
Case 2 : 'Other table add
End Select
End If
End If
End Sub
我想要做的就是:
Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
Dim bList As BindingList(Of Object) = TryCast(sender, BindingList(Of Object))
Dim m As Object = bList.LastOrDefault()
'somehow get the table (type) of the entry through the m object
If m IsNot Nothing Then
db.<Table>.Add(m)
End If
End If
End Sub
因此,不必为每个案例编写每个添加内容,我只需要做类似的事情。 是否有可能或我是否会坚持选择案例?
提前致谢,对不起,如果我的英语不好(我不是本地人)。
编辑1: 正如Mark在评论中提到的,我们可以在C#中使用this 但在VB中它不起作用......
Public Class GenericRepository(Of T)
Implements IDisposable
Friend context As GestProGest1Entities
Friend dbSet As Entity.DbSet(Of T) ' Gives error on T "Type argument 'T' does not satisfy the 'Class' constraint for type parameter 'TEntity'"
Public Sub Dispose() Implements IDisposable.Dispose
If context IsNot Nothing Then
context.Dispose()
context = Nothing
End If
End Sub
Public Sub New(context As GestProGest1Entities)
Me.context = context
Me.dbSet = context.Set(Of T)() ' Gives error on T "Type argument 'T' does not satisfy the 'Class' constraint for type parameter 'TEntity'"
End Sub
Public Overridable Sub Insert(entity As T)
dbSet.Add(entity)
context.SaveChanges()
End Sub
End Class
如何在VB中做到这一点?
编辑2: 好的,所以我让它像这样工作
Public Class GenericRepository(Of T As Class)
现在我的问题是如何从对象中获取类型
Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
Dim bList As BindingList(Of Object) = TryCast(sender, BindingList(Of Object))
Dim m As Object = bList.LastOrDefault()
Dim myType As Type = m.GetType()
Dim table As New GenericRepository(Of myType)(db) 'Doesn't accept myType here...
table.Insert(m)
End If
End Sub
答案 0 :(得分:1)
在Mark的帮助下,我终于有了这个工作。
Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
Dim m As Object = sender(sender.count - 1)
db.Set(m.GetType()).Add(m)
End If
End Sub
感谢大家的帮助!