2

我正在使用几个代码转换器,它们产生相同的 VB.Net 编码,但除了这行代码之外,VS 不会:

Private Overridable m_Products As ICollection(Of Product)

VS 状态:

'Overridable' 在成员变量声明中无效。

C# 编码来自 ASP.Net 网站上的教程:

http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/create_the_data_access_layer

VS 还声明我应该删除 Overridable 关键字。如果我这样做,我会破坏教程中的内容吗?

这是我通过转换器运行的 C# 编码:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace WingtipToys.Models
{
    public class Category
    {
        [ScaffoldColumn(false)]
        public int CategoryID { get; set; }

        [Required, StringLength(100), Display(Name = "Name")]
        public string CategoryName { get; set; }

        [Display(Name = "Product Description")]
        public string Description { get; set; }

        public virtual ICollection<Product> Products { get; set; }
    }
}

这是转换的结果:

Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations

Namespace WingtipToys.Models
Public Class Category
    <ScaffoldColumn(False)> _
    Public Property CategoryID() As Integer
        Get
            Return m_CategoryID
        End Get
        Set
            m_CategoryID = Value
        End Set
    End Property
    Private m_CategoryID As Integer

    <Required, StringLength(100), Display(Name := "Name")> _
    Public Property CategoryName() As String
        Get
            Return m_CategoryName
        End Get
        Set
            m_CategoryName = Value
        End Set
    End Property
    Private m_CategoryName As String

    <Display(Name := "Product Description")> _
    Public Property Description() As String
        Get
            Return m_Description
        End Get
        Set
            m_Description = Value
        End Set
    End Property
    Private m_Description As String

    Public Overridable Property Products() As ICollection(Of Product)
        Get
            Return m_Products
        End Get
        Set
            m_Products = Value
        End Set
    End Property
    Private Overridable m_Products As ICollection(Of Product)
End Class
End Namespace
4

2 回答 2

5

您可以创建一个Property Overridable,但不能创建一个Field

' The property here is fine to be overridable
Public Overridable Property Products() As ICollection(Of Product)
    Get
        Return m_Products
    End Get
    Set
        m_Products = Value
    End Set
End Property

' The backing field cannot be
Private m_Products As ICollection(Of Product)

派生类可以重新实现和覆盖属性,这可能会导致支持字段未被使用,但它们不能直接覆盖字段。

于 2013-05-21T16:45:04.050 回答
3

字段不能被覆盖,但属性可以。我不确定为什么转换器会创建一个支持字段,您可以使用 VB.NET 的自动属性(如果您使用的是 Visual Studio 2010 或更高版本)。尝试这个:

Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations

Namespace WingtipToys.Models
Public Class Category
    <ScaffoldColumn(False)> _
    Public Property CategoryID As Integer

    <Required, StringLength(100), Display(Name := "Name")> _
    Public Property CategoryName As String


    <Display(Name := "Product Description")> _
    Public Property Description As String         

    Public Overridable Property Products As ICollection(Of Product)
End Class
End Namespace
于 2013-05-21T16:50:05.257 回答