簡體   English   中英

重載解析失敗,因為沒有可訪問的“新”是這些參數最具體的:

[英]Overload resolution failed because no accessible 'New' is most specific for these arguments:

我有一個帶中繼器的用戶控件。 最初在頁面加載時,我有從數據庫中獲取數據並綁定到轉發器的代碼。 我現在想把這個功能放在用戶控件之外,這樣我就可以在頁面上有多個,並將它們綁定到不同的數據。

我現在的代碼是:

Imports System.ComponentModel

Public Class UpdateList
    Inherits System.Web.UI.UserControl

    Private m_dataSource As Object

    <TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")> _
    <Category("Data")> _
    <DefaultValue(Nothing)> _
    Public Property DataSource() As Object
        Get
            Return Me.m_dataSource
        End Get
        Set(value As Object)
            If Me.m_dataSource <> value Then
                m_dataSource = value
                tryDataBinding()
            End If
        End Set
    End Property

    Public ReadOnly Property UpdateCount As Integer
        Get
            Return m_UpdateCount
        End Get
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    End Sub

    Protected Sub tryDataBinding()

        rep_Updates.DataSource = Me.m_dataSource
        rep_Updates.DataBind()

    End Sub

End Class

我在<DefaultValue(Nothing)>處得到一條波浪線並得到一個錯誤:

重載解析失敗,因為沒有可訪問的“新”是這些參數最具體的:

'Public Sub New(value As Boolean)':不是最具體的。

'Public Sub New(value As Byte)':不是最具體的。

'Public Sub New(value As Char)':不是最具體的。

這是什么意思? 謝謝

更新

解決方法是將數據源的屬性聲明更改為...

    Private m_dataSource As Object

    Public Property DataSource() As Object
        Get
            Return Me.m_dataSource
        End Get
        Set(value As Object)
            m_dataSource = value
            tryDataBinding()
        End Set
    End Property

至少在WinForms中 ,DefaultValue屬性ctor不能為Nothing (在對象瀏覽器中沒有這樣的定義)。 DefaultAttribute並未定義初始起始值(盡管有名稱),但是定義了何時保留屬性值的比較值。 無論如何,對於Web表單和數據源來說,這似乎都是可疑的,因此只需刪除該屬性即可。

如您所述, TypeConverter可能也TypeConverter

我在<DefaultValue(Nothing)>處出現波浪線,並[…]出現錯誤:

重載解析失敗,因為對於這些參數,沒有可訪問的“新建”是最具體的:

Public Sub New(value As Boolean) :不是最具體的。
Public Sub New(value As Byte) :並非最具體。
Public Sub New(value As Char) :不是最具體。

DefaultValueAttribute構造函數已重載。 此錯誤意味着VB.NET編譯器難以確定要為<DefaultValue(Nothing)>調用哪些重載。 問題在於,VB.NET中的Nothing實際上都不能表示兩件事:

  • 空引用(在C#中為null
  • 為其分配類型的默認值default(T) C#中的default(T)

因此,可以選擇可用的構造函數重載中的每個重載。 您顯然打算讓VB.NET選擇Public Sub New(value As Object)重載 ,但是編譯器根本不夠聰明,無法識別這一點。

不幸的是,使Nothing變得更具體似乎是不可能的,例如通過CObj(Nothing) ,因為只允許使用常量值作為自定義屬性參數。

如果找到此VB.NET特定問題的解決方案,則將更新答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM