Reputation: 17794
I have a partial class being generated by a tool.
Foo.cs
public partial class Foo {
[SomeAttribute()]
public string Bar {get;set;}
}
I need to implement the following interface for Foo
without touching Foo.cs:
IFoo.cs
public interface IFoo {
string Bar {get;set;}
}
Extending Foo
is also an option, but re-implementing the Bar
property isn't.
Can this be done?
Upvotes: 5
Views: 3542
Reputation: 48975
As Bar
is private, here's what you're looking for:
public partial class Foo : IFoo
{
string IFoo.Bar
{
get
{
return this.Bar; // Returns the private value of your existing Bar private field
}
set
{
this.Bar = value;
}
}
}
Anyway, this is confusing and should be avoided if possible.
EDIT: Okay, you've changed your question, so as Bar
is now public, there is no more problem as Bar
is always implemented in Foo
.
Upvotes: 1
Reputation: 2861
You could create a partial class for Foo that implements IFoo but with the Bar property not being public it won't work.
If Bar property was public:
partial class Foo
{
public string Bar { get; set; }
}
interface IFoo
{
string Bar { get; set; }
}
partial class Foo : IFoo
{
}
Upvotes: 1
Reputation: 12346
What prevents you from doing this again in another file?
public partial class Foo : IFoo
{
}
Since Bar
property already exists, it wont be needed to reimplement it.
Or in a new class
public class FooExtended : Foo, IFoo
{
}
Again, you won't need to implement Bar
since Foo implements it already.
Upvotes: 8