访问部分类的另一半的成员
我刚刚学习如何使用 VB.NET 和 VS2008 中的部分类。 具体来说,我正在尝试扩展由 SqlMetal 自动创建的 LINQ to SQL 类。
自动生成的类如下所示:
Partial Public Class DataContext
Inherits System.Data.Linq.DataContext
...
<Table(Name:="dbo.Concessions")> _
Partial Public Class Concession
...
<Column(Storage:="_Country", DbType:="Char(2)")> _
Public Property Country() As String
...
End Property
...
End Class
在一个单独的文件中,这就是我想要做的事情:
Partial Public Class DataContext
Partial Public Class Concession
Public Function Foo() as String
Return DoSomeProcessing(Me.Country)
End Function
End Class
End Class
...但我在“Me.Country
”下看到蓝色锯齿和消息 < code>“Country”不是“DataContext.Concession”的成员。 分部类的两半都位于同一名称空间中。
那么,如何从我的部分类的一半访问自动生成的部分类的属性呢?
I'm just learning to work with partial classes in VB.NET and VS2008. Specifically, I'm trying to extend a LINQ to SQL class that was automatically created by SqlMetal.
The automatically generated class looks like this:
Partial Public Class DataContext
Inherits System.Data.Linq.DataContext
...
<Table(Name:="dbo.Concessions")> _
Partial Public Class Concession
...
<Column(Storage:="_Country", DbType:="Char(2)")> _
Public Property Country() As String
...
End Property
...
End Class
In a separate file, here's what I'm trying to do:
Partial Public Class DataContext
Partial Public Class Concession
Public Function Foo() as String
Return DoSomeProcessing(Me.Country)
End Function
End Class
End Class
... but I get blue jaggies under 'Me.Country
' and the message 'Country' is not a member of 'DataContext.Concession'
. Both halves of the partial class are in the same namespace.
So how do I access the properties of the automatically-generated half of the partial class, from my half of the partial class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非 VB.NET 在其 LINQ to SQL 文件中从 C# 生成不同的内容,否则数据库表的类不在 DataContext 类中,就在它旁边。
因此,当部分类的另一半确实是 MyNamespace.Concession 时,您就拥有了类 MyNamespace.DataContext.Concession
Unless VB.NET generates different stuff in its LINQ to SQL files from C# the classes of the DB tables aren't within the DataContext class, just beside it.
So you have the class MyNamespace.DataContext.Concession when the other half of the partial class is realy MyNamespace.Concession
(这与 VB.NET 相关 - 可能与 C# 项目有所不同)
我通过配置 Linq-to-SQL 模型属性将实体放入它们自己的命名空间中。
例如 MyCo.MyProj.Business.Entities
然后我也在其中添加非 Linq 业务实体,因此它们都位于同一名称空间中。
然而,当尝试添加上述部分类时,我发现部分类(即您生成的类,而不是自动生成的 LINQ 类)必须与 Linq-to-SQL 模型位于同一项目中。 否则,在类视图和对象查看器中,您会看到两个单独的类 - 看似位于同一名称空间,但实际上并非如此。 不确定这是一个错误还是我做错了什么。
但是,无论如何,将部分类文件放在与模型工作相同的项目中。
(This related to VB.NET - might be differences with C# projects)
I put my entities in their own namespace by configuring the Linq-to-SQL model property.
e.g. MyCo.MyProj.Business.Entities
I then add non-Linq business entities in there too, so they are all in the same namespace.
However, when trying to do the above partial class additions, I found that the partial class (i.e. the one you generate, not the auto-generated LINQ class) MUST be in the same project as the Linq-to-SQL model. Otherwise in the Class View and Object Viewer you see two separate classes - seemingly in the same namespace, but not really. Not sure if this is a bug or I am doing something wrong.
But, anyway, putting the partial class file in the same project as your model works.