Object initializer with explicit interface in C#
Asked Answered
V

2

11

How can I use an object initializer with an explicit interface implementation in C#?

public interface IType
{
  string Property1 { get; set; }
}

public class Type1 : IType
{
  string IType.Property1 { get; set; }
}

...

//doesn't work
var v = new Type1 { IType.Property1 = "myString" };
Viviennevivify answered 5/4, 2010 at 11:42 Comment(4)
Can you tell us why you wish to do so?Quadrille
I recommend my doctor's advice: "if it hurts, don't do it".Grimaud
"Don't do it" never inspires innovation.Extirpate
Reason for question: I have a type that implements an interface explictly, which I'd like to initialize using the object intiializer syntax for terseness. I find explicit interface implementation to be less error prone and more readable.Viviennevivify
L
4

You can't. The only way to access an explicit implementation is through a cast to the interface. ((IType)v).Property1 = "blah";

You could theoretically wrap a proxy around the property, and then use the proxy property in initialization. (The proxy uses the cast to the interface.)

class Program
{
    static void Main()
    {
        Foo foo = new Foo() { ProxyBar = "Blah" };
    }
}

class Foo : IFoo
{
    string IFoo.Bar { get; set; }

    public string ProxyBar
    {
        set { (this as IFoo).Bar = value; }
    }
}

interface IFoo
{
    string Bar { get; set; }
}
Lagomorph answered 5/4, 2010 at 11:57 Comment(0)
C
4

Explicit interface methods/properties are private (this is why they cannot have an access modifier: it would always be private and so would be redundant*). So you can't assign to them from outside. You might as well ask: how can I assign to private properties/fields from external code?

(* Though why they didn't make the same choice with public static implicit operator is another mystery!)

Chump answered 5/4, 2010 at 11:54 Comment(3)
Please expand upon description as "private". Explicit methods can be invoked by other objects, which seems at odds with your description?!Viviennevivify
I think I understand that explicitly implemented members are private to the implementing type. AFAICT it is merely a language syntax limitation preventing the convenient setting of explicit members, which is a shame, seeing as I prefer explicit over implicit.Viviennevivify
@Ben Aston - "private" isn't the right word in all respects, e.g. a mere private field can by accessed inside the class whereas an explicit interface member cannot even allow that(directly). It would be more correct to say that explicit interface members are not accessible at all via the type they are implemented in (so even more private than private!) You can only access them via the interface type itself. Object initializer syntax is effectively just a way of assigning to members, and it would be weird if that could achieve things that ordinary assignments could not.Chump
L
4

You can't. The only way to access an explicit implementation is through a cast to the interface. ((IType)v).Property1 = "blah";

You could theoretically wrap a proxy around the property, and then use the proxy property in initialization. (The proxy uses the cast to the interface.)

class Program
{
    static void Main()
    {
        Foo foo = new Foo() { ProxyBar = "Blah" };
    }
}

class Foo : IFoo
{
    string IFoo.Bar { get; set; }

    public string ProxyBar
    {
        set { (this as IFoo).Bar = value; }
    }
}

interface IFoo
{
    string Bar { get; set; }
}
Lagomorph answered 5/4, 2010 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.