添加一个接口部分类 [英] Adding an interface to a partial class

查看:102
本文介绍了添加一个接口部分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由第三方工具生成的类:

I have a class that is generated by a third party tool:

public partial class CloudDataContext : DbContext 
{
    // ...SNIPPED... 
    public DbSet<User> Users { get; set; } 

}



我创建一个部分类,然后分配接口,所以我可以注入该类更高版本:

I create a partial class and then assign an interface so that I can inject this class later:

public partial class CloudDataContext : IDataContext
{

}   



IDataContext 具有单一的财产用户

这不会编译,编译器抱怨接口没有实现。

This won't compile, the compiler complains that the interface isn't implemented.

如果我接口移动到生成的类,它工作正常。我不能这样做,但因为它是生成的代码。

If I move the interface to the generated class, it works fine. I can't do that though as it's generated code.

如何申请一个接口部分类揭露如上定义的类?

How can I apply an interface to a partial class to expose the class as defined above?

推荐答案

这个问题必须在其他地方,因为你可以在部分,另一部分实现接口类,那么它的设置的。我只是尝试下,它编译就好了:

The problem must be somewhere else, because you can implement interface in the other part of partial class then it's set on. I just tried following and it compiles just fine:

public interface IFoo
{
    int Bar { get; set; }
}

public partial class Foo
{
    public int Bar { get; set; }
}

public partial class Foo : IFoo
{

}

属性可能使用接口不同的类型。

The properties probably use different types in interface and class.

这篇关于添加一个接口部分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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