Ben*_*Ben 10 c# explicit-interface
如何在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" };
Run Code Online (Sandbox Code Playgroud)
你不能。访问显式实现的唯一方法是通过对接口的强制转换。((IType)v).Property1 = "blah";
理论上,您可以在该属性周围包装一个代理,然后在初始化中使用该代理属性。(代理使用接口的强制转换。)
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; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4414 次 |
| 最近记录: |