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

查看:89
本文介绍了你能在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类impment和接口?提前致谢!

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天全站免登陆