我有一个.NET 2010项目。在其中,我创建了一个基本的用户类。实例化它并成功填充数据。它一直在这样做,如果我将鼠标悬停在课堂上,它就会说它什么都没有,即使它填满了它的属性。后来,它在UI中输给我,即使该属性有一个值,它表示该类没什么。
我要安排上课......
Dim oExtendedUser As New ExtendedUser
这是课程定义......
Public Class ExtendedUser
Inherits System.Web.Security.MembershipUser
Public Sub New()
_Role = New Role
End Sub
Public Property ExtendedUserID As Int32
Public Property FirstName As String
Public Property LastName As String
Public Property Phone As String
Public Property UserID As Guid
Public Property Role As Role
Public Property UserName() As String
Public Property Password() As String
Public Property SecurityQuestion() As String
Public Property SecurityAnswer() As String
End Class
我改变了课程。我添加了MyBase.New(),但问题仍然存在。在UI上,这是单击按钮时执行的代码。作为一个支持者,Director拥有ExtendedUSer
Dim oCase As New BE.Case
Dim oDirector As New BE.Director
oDirector = SessionManager.Director 'the values are here
oCase.Investigator.ExtendedUserID = oDirector.ExtendedUser.ExtendedUserID
这是导演......
Public Class Director
Public Sub New()
_ExtendedUser = New ExtendedUser
End Sub
Public Property ID As Int32
Public Property ExtendedUser As ExtendedUser
End Class
答案 0 :(得分:4)
你被ToString Override欺骗了。 您的对象存在,但它会覆盖ToString方法:MembershipUser.ToString
要验证此行为,请尝试使用简单的类:
VB.NET
Public Class Test
Public Property TestString As String
Public Overrides Function ToString() As String
Return Me.TestString
End Function
End Class
C#
public class Test
{
public string TestString { get; set; }
public override string ToString()
{
return this.TestString;
}
}
使用此代码,Watch将显示一个instanciated Test to Nothing,因为ToString值将为Nothing。该对象存在,但Visual Studio使用ToString方法填充值字段,此时它是Nothing。
VB.NET
Public Class Test
Public Property TestString As String = ""
Public Overrides Function ToString() As String
Return Me.TestString
End Function
End Class
C#
public class Test
{
public Test()
{
this.TestString = "";
}
public string TestString { get; set; }
public override string ToString()
{
return this.TestString;
}
}
使用此代码,您将获得一个空字符串。
要回到您的代码,您无法扩展MembershipUser,因此您必须遵循以下准则:How to: Implement a Custom Membership User。由于许多内容不适用于您的实际扩展(例如,您的用户名遮蔽了基本扩展名)。
同样请参阅this question。扩展用户“实体”比继承更简单。