如何扩展 MembershipUser 类而不使用多重继承?

发布于 2024-08-04 16:53:33 字数 208 浏览 1 评论 0原文

在顶层我有一个 Person 类。接下来我希望 .NET 的 MembershipUser 类继承它,因为成员是一个人。但是,我想扩展 MembershipUser 类,这意味着,我认为,我需要创建自己的 MembershipUser 类,该类继承自 MembershipUser,然后添加我自己的属性。但是一旦我这样做了,由于多重继承规则(ASP.NET 2.0),我就不能再从 Person 继承。

At the top level I have a Person class. Next I want .NET's MembershipUser class to inherit from it, because a member is a person. However I want to extend the MembershipUser class which means, I think, I need to create my OWN MembershipUser class which inherits from MembershipUser and then adds my own properties. But once I do that, I can no longer inherit from Person due to multiple inheritance rules (ASP.NET 2.0).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心舞飞扬 2024-08-11 16:53:33

您可以通过创建一个接口 IPerson 并将具体的 Person 类替换为继承自 MembershipUser 并实现 IPerson 的类来解决此问题

您还可以保留具体的 Person,创建 IPerson 并让您自己的类封装 Person 实例并从 MembershipUser 继承> 在实现IPerson时。

无论哪种情况,您曾经使用过具体 Person 类型的任何地方都应该替换为 IPerson(例如方法参数)。

interface IPerson
{
    string LastName { get; set; }
    // ...
}

class MyMembershipUser : MembershipUser, IPerson
{
    private Person _person = new Person();

    // constructors, etc.

    public string LastName
    {
        get { return _person.LastName; }
        set { _person.LastName = value; }
    }
}

或者,您可以继续使用 Person 并让它封装一个 MembershipUser 实例(作为构造函数的一部分),并在需要时将 Person 显式转换为 MembershipUser...

class Person
{
    private readonly MembershipUser _mu;
    public Person(MembershipUser mu)
    {
        _mu = mu;
    }

    public static explicit operator MembershipUser(Person p)
    {
        // todo null check
        return p._mu;
    }
}

// example
var person = new Person(Membership.GetUser("user"));
Membership.UpdateUser((MembershipUser)person);

我会自己使用接口实现解决方案。

You can solve this by creating an interface, IPerson and replacing your concrete Person class with a class which inherits from MembershipUser and implements IPerson.

You could also keep your concrete Person, create IPerson and have your own class encapsulate a Person instance and inherit from MembershipUser while implementing IPerson.

In either case, anywhere where you once used a concrete Person type you should replace with IPerson (such as method arguments).

interface IPerson
{
    string LastName { get; set; }
    // ...
}

class MyMembershipUser : MembershipUser, IPerson
{
    private Person _person = new Person();

    // constructors, etc.

    public string LastName
    {
        get { return _person.LastName; }
        set { _person.LastName = value; }
    }
}

Alternatively you could continue using Person and have it encapsulate a MembershipUser instance (as part of a constructor) and include an explicit cast for Person to MembershipUser when needed...

class Person
{
    private readonly MembershipUser _mu;
    public Person(MembershipUser mu)
    {
        _mu = mu;
    }

    public static explicit operator MembershipUser(Person p)
    {
        // todo null check
        return p._mu;
    }
}

// example
var person = new Person(Membership.GetUser("user"));
Membership.UpdateUser((MembershipUser)person);

I would go with the interface implementation solution myself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文