无法将 MembershipUser 转换为自定义类 (ASP.NET)
我使用默认的 SqlMembershipProvider,但我创建了一个自定义 MembershipUser 类 (SoeMembershipUser),因为我需要一个“DisplayName”属性。 DisplayName 所做的只是查看 UserName 并对其进行不同的格式化。
当我尝试将 MembershipUser 转换为 SoeMembershipUser 用户时,我收到 InvalidCastException。确切的错误是: “无法将“System.Web.Security.MembershipUser”类型的对象转换为“Soe.Tracker.SoeMembershipUser”类型。”
这是失败的代码:
SoeMembershipUser user = (SoeMembershipUser)Membership.GetUser(username); // invalid cast
我也尝试稍后进行转换,如下所示:
MembershipUser user = Membershipship.GetUser(username); // ok
...
string displayName = ((SoeMembershipUser)user).DisplayName; // invalid cast
这是 SoeMembershipUser 类: 注意:我一开始放弃了构造函数,但后来当我开始遇到问题时添加了它们。添加它们没有什么区别。
public class SoeMembershipUser : MembershipUser
{
public SoeMembershipUser()
: base()
{
}
public SoeMembershipUser(string providerName, string name,
Object providerUserKey, string email, string passwordQuestion,
string comment, bool isApproved, bool isLockedOut,
DateTime creationDate, DateTime lastLoginDate,
DateTime lastActivityDate, DateTime lastPasswordChangedDate,
DateTime lastLockoutDate)
: base(providerName, name, providerUserKey, email,
passwordQuestion, comment, isApproved, isLockedOut,
creationDate, lastLoginDate, lastActivityDate,
lastPasswordChangedDate, lastLockoutDate)
{
}
public virtual string DisplayName
{
get
{
if (UserName.Contains("@"))
return UserName.Substring(0, UserName.IndexOf("@"));
return UserName;
}
}
}
知道为什么这个演员无效吗?我只是忽略了一些简单的事情吗?
I'm using the default SqlMembershipProvider, but I've created a custom MembershipUser class (SoeMembershipUser) because I needed a "DisplayName" property. All the DisplayName does is look at the UserName and format it differently.
When I try to cast a MembershipUser to a SoeMembershipUser user I get an InvalidCastException. Exact error is:
"Unable to cast object of type 'System.Web.Security.MembershipUser' to type 'Soe.Tracker.SoeMembershipUser'."
Here is the code that fails:
SoeMembershipUser user = (SoeMembershipUser)Membership.GetUser(username); // invalid cast
I have also tried casting later like so:
MembershipUser user = Membershipship.GetUser(username); // ok
...
string displayName = ((SoeMembershipUser)user).DisplayName; // invalid cast
Here is the SoeMembershipUser class:
NOTE: I left off the constructors at first, but added them in later when I started having problems. Adding them made no difference.
public class SoeMembershipUser : MembershipUser
{
public SoeMembershipUser()
: base()
{
}
public SoeMembershipUser(string providerName, string name,
Object providerUserKey, string email, string passwordQuestion,
string comment, bool isApproved, bool isLockedOut,
DateTime creationDate, DateTime lastLoginDate,
DateTime lastActivityDate, DateTime lastPasswordChangedDate,
DateTime lastLockoutDate)
: base(providerName, name, providerUserKey, email,
passwordQuestion, comment, isApproved, isLockedOut,
creationDate, lastLoginDate, lastActivityDate,
lastPasswordChangedDate, lastLockoutDate)
{
}
public virtual string DisplayName
{
get
{
if (UserName.Contains("@"))
return UserName.Substring(0, UserName.IndexOf("@"));
return UserName;
}
}
}
Any idea why this cast is invalid? Am I just overlooking something simple?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试从基类向下转换为派生类(也称为缩小转换,如从动物到狗 - 但并非所有动物都是狗)。创建一个扩展方法 ToMembershipUser,它将执行转换,因为您知道它应该如何进行,而编译器不知道。
You are trying to downcast from a base to a derived class (aka a narrowing conversion, as from an Animal to a Dog -- but not every animal is a dog). Create an extension method, ToMembershipUser, that will do the conversion since you know how it should proceed and the compiler doesn't.
问题是您正在使用默认的 SqlMembershipProvider。您必须创建一个自定义 MembershipProvider。
您可能只需扩展 GetUser 方法上的 SqlMembershipProvider 即可。
The problem is that you are using the default SqlMembershipProvider. You will have to create a custom MembershipProvider.
You can probably get by with just extending the SqlMembershipProvider on the GetUser methods.