Adding an interface to a partial class
Asked Answered
M

5

18

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
{

}   

The IDataContext has the single property Users.

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?

Middlemost answered 11/12, 2013 at 0:31 Comment(3)
Does DBContext not implement IDataContext?Hornbeck
Can you show the definition of IDataContext and the implementation of CloudDataContext? I just ran this example and it works just fine.Signalman
I'm going to go out on a pretty far limb here: are the using directives at the top of the two class files different? Is it possible that in the one code file, IDataContext resolves to one interface, and in the other code file IDataContext resolves to a second different interface?Bisulfate
E
34

The problem must be somewhere else, because you can implement interface in the other part of partial class than 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.

Enceinte answered 11/12, 2013 at 0:35 Comment(2)
Turns out it was a typo in the name space of one of the partials. Once matched, it worked.Middlemost
In my case it was because the new partial class file I generated had a different class name. Remember to change the class name to match the one in the other file...Scene
E
5

Here's a quick checklist. Do the classes have identical:

  • Names?
  • Namespaces?
  • Access modifiers?

Example:

  • You decide to split an existing class into two files.
  • The original file's namespace doesn't match its folder path.
  • Consequently, the new class file you create has a mismatching namespace.
  • Build fails.
Escort answered 13/11, 2018 at 20:8 Comment(0)
L
1

IN my Case problem was that interface Method that was implemented in other part of the partial class was not compiling and C# was giving error of not implemented Method

Landy answered 23/10, 2020 at 7:39 Comment(0)
T
0

In my case, the interface method signature didn't mention a value (uint direction) that the actual method expected. This showed up as the interface having errors in one of the partial classes. Make sure that the interface for a method is actually the same as the method signature itself. D'oh.

Thrawn answered 20/6, 2021 at 17:36 Comment(0)
L
0

Make sure that all files of the partial class have "C# compiler" Build Action in VS.

Lightfoot answered 25/6, 2023 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.