你能在 Linq2Sql 类上实现一个接口吗? [英] Can you implement an interface on a Linq2Sql class?

查看:23
本文介绍了你能在 Linq2Sql 类上实现一个接口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 IAddress 的接口和一个名为 Address 的类,用于处理街道、城市、州/省、邮政编码和国家/地区.我有几个具有所有地址信息的 Linq2Sql 类,并希望实现接口 IAddress,并将其传递给 Address 的构造函数以加载属性值.

I have an interface called IAddress, and a class called Address that handles street, city, state/province, postal code and country. I have a couple of Linq2Sql classes that has all the address information and would like to implement the interface IAddress, and pass that in to the constructor for Address that would the load the property values.

是否可以通过我为它创建的部分类来实现 Linq2Sql 类的实现和接口?提前致谢!

Is it possible have a Linq2Sql class impment and interface through the partial class that I created for it? Thanks in advance!

其他评论

在我的班级中,我有一个名为 MailToStreet 的属性,我希望将其映射到 IAddress.Street.有没有办法在部分类中做到这一点?

In my class I have a property called MailToStreet, I want that to map to IAddress.Street. Is there a way to do this in the partial class?

已解决

感谢 StackOverflow 社区!这是一个瞬间!这是我的最终代码:

Thanks StackOverflow community! It was a snap! Here is my final code:

public partial class Location : IAddress
{
    string IAddress.Street
    {
        get { return this.Street; }
        set { this.Street = value; }
    }

    string IAddress.City
    {
        get { return this.City; }
        set { this.City = value; }
    }

    string IAddress.StateProvince
    {
        get { return this.StateProvince; }
        set { this.StateProvince = value; }
    }

    string IAddress.PostalCode
    {
        get { return this.PostalCode; }
        set { this.PostalCode = value; }
    }

    string IAddress.Country
    {
        get { return this.Country; }
        set { this.Country = value; }
    }
}

推荐答案

LinqToSQL 类是部分类,因此您可以有一个额外的文件来实现 LinqToSQL 类的接口.

LinqToSQL classes are partial classes, so you can have an additional file that implements the interface for the LinqToSQL class.

只需将其添加到一个新文件中,使用与您的 LinqToSQL 类相同的类名:

Just add this to a new file, using the same class name as your LinqToSQL class:

public partial class LinqToSqlClass : IFoo {
    public void Foo() {
        // implementation
    }
}

如果您的 LinqToSQL 类已经实现了必要的属性,您应该只能包含接口声明.

If your LinqToSQL class already implements the necessary proporties you should be able to only include the interface declaration.

要回答关于使用不同名称的 LinqToSQL 属性来实现接口的评论,您可以使用上面的语法并从接口属性调用 LinqToSQL 属性,或者为了使事情更清晰,使用显式实现:

To answer the comment about using a differently-named LinqToSQL property to implement the interface, you can use the syntax above and just call the LinqToSQL property from the interface property, or to keep things a bit cleaner, use explicit implementation:

public partial class LinqToSqlClass : IFoo {
    void IFoo.Foo() {
        return this.LinqFoo(); // assumes LinqFoo is in the linq to sql mapping
    }
}

通过使用此语法,访问您的类的客户端将不会看到仅用于实现接口的冗余属性(除非将对象强制转换为该接口,否则它将不可见)

By using this syntax, clients accessing your class will not see a redundant property used only for implementing an interface (it will be invisible unless the object is cast to that interface)

这篇关于你能在 Linq2Sql 类上实现一个接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆