委托on get accessor抛出不兼容的类型错误

时间:2018-06-17 19:06:48

标签: vb.net delegates

我一直在研究这个问题,但似乎无法使其发挥作用......

有一个大的sql表(很多列,行)我必须得到excel的值,我使用LINQ to SQL获取每一行并使用Reflection按名称迭代每一列并将其写入适当的excel列。看到反射很慢,我试图将委托分配给每个属性并将其(通过字典)映射到列名以避免开销。

问题: 在VB.NET中,当我尝试将委托分配给属性的GetMethod(比如整数)时,这样:

Dim prop  = GetType(TestSQLClass).GetProperty("SomeColumn")
Dim del As [Delegate] = [Delegate].CreateDelegate(GetType(Func(Of Integer)), prop.GetMethod)

我收到此错误:

  

无法绑定到目标方法,因为它的签名或安全性   透明度与委托类型的透明度不兼容。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

感谢您的提问。我认为在C#中找到一个并且找到一个是好的。请参阅模块底部的属性。我喜欢DataType扩展的想法。它甚至出现在intellisense中。大约1500条记录,10列,主要是文本,在3秒内完成。

Imports Microsoft.Office.Interop
Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
Module DataTableToExcel
    ' Export DataTable into an excel file with field names in the header line
    ' - Save excel file without ever making it visible if filepath Is given
    ' - Don't save excel file, just make it visible if no filepath is given
    Private excelApp As Excel.Application
    Private workSheet As Excel.Worksheet
    <Extension>
    Public Sub ExportToExcel(tbl As DataTable, excelFilePath As String)
        Dim sw As New Stopwatch
        sw.Start()
        Try
            If IsNothing(tbl) OrElse tbl.Columns.Count = 0 Then
                Throw New Exception("ExportToExcel: Null or empty input table!")
            End If
            ' load excel, And create a New workbook
            excelApp = New Excel.Application()
            excelApp.Workbooks.Add()
            ' single worksheet
            workSheet = CType(excelApp.ActiveSheet, Excel.Worksheet)
            ' column headings
            For i = 0 To tbl.Columns.Count - 1
                workSheet.Cells(1, i + 1) = tbl.Columns(i).ColumnName
            Next
            Dim CellArray(tbl.Rows.Count - 1, tbl.Columns.Count - 1) As Object
            'rows
            For i = 0 To tbl.Rows.Count - 1
                'to do: Format DateTime values before printing
                For j = 0 To tbl.Columns.Count - 1
                    CellArray(i, j) = tbl.Rows(i)(j)
                Next
            Next
            workSheet.Range((workSheet.Cells(2, 1)), (workSheet.Cells(tbl.Rows.Count + 1, tbl.Columns.Count))).Value = CellArray
            ' check file path
            If Not String.IsNullOrEmpty(excelFilePath) Then
                Try
                    excelApp.DisplayAlerts = False
                    workSheet.SaveAs(excelFilePath)
                    MessageBox.Show("Excel file saved!")
                Catch ex As Exception
                    Throw New Exception("ExportToExcel: Excel file could not be saved! Check filepath." & ex.Message)
                End Try
            Else  ' no file path Is given
                excelApp.Visible = True
            End If
        Catch ex As Exception
            Throw New Exception("ExportToExcel:" & ex.Message)
        Finally
            excelApp.Workbooks.Close()
            excelApp.Quit()
            Marshal.ReleaseComObject(workSheet)
            Marshal.ReleaseComObject(excelApp.Workbooks)
            Marshal.ReleaseComObject(excelApp)
        End Try
        sw.Stop()
        Debug.Print(sw.ElapsedMilliseconds.ToString)
    End Sub
    'contributed by tuncalik; https://stackoverflow.com/questions/8207869/how-to-export-datatable-to-excel
    'speed improvement by Tomasz Wiśniewski
    'Releasing objects provided by  GrammatonCleric
End Module

Imports System.Data.SqlClient
Public Class TestDataTableToExcel
    Private dt As New DataTable
    Private Sub btnCreateExcelFile_Click(sender As Object, e As EventArgs) Handles btnCreateExcelFile.Click
        dt.ExportToExcel("Coffee.xlsx")
    End Sub

    Private Sub btnCreateDataTable_Click(sender As Object, e As EventArgs) Handles btnCreateDataTable.Click
        Using cn As New SqlConnection(My.Settings.CoffeeConnectionString)
            Using cmd As New SqlCommand("Select * From Coffees;", cn)
                cn.Open()
                Using dr As SqlDataReader = cmd.ExecuteReader
                    dt.Load(dr)
                End Using
            End Using
        End Using
    End Sub
End Class