使用仅 getter 自动 属性 接口的显式实现(C# 6 功能)
Explicit implementation of an interface using a getter-only auto-property (C# 6 feature)
为显式接口实现使用自动属性was not possible in C# 5, but now that C# 6 supports getter-only auto-properties,现在应该可以了吧?
在 C# 6 中创建自动 属性 成功,但是当试图在构造函数中为其赋值时,您必须先将 this
转换为接口类型,因为实现是明确的。但这就是 VS 2015 RC 和 VS Code 0.3.0 显示可以在评论中看到的错误的地方:
using static System.Console;
namespace ConsoleApp
{
public interface IFoo { string TestFoo { get; } }
public class Impl : IFoo
{
// This was not possible before, but now works.
string IFoo.TestFoo { get; }
public Impl(string value)
{
// ERROR: Property or indexer 'IFoo.TestFoo' cannot be assigned to -- it is read only.
((IFoo)this).TestFoo = value;
}
}
public class Program
{
// Yes, not static. DNX supports that (for constructor DI).
public void Main(string[] args)
{
IFoo foo = new Impl("World");
WriteLine($"Hello {foo.TestFoo}");
ReadKey(true);
}
}
}
注意:我将设置常量值的原始问题更新为 TestFoo
。在我的真实场景中,值来自注入到构造函数中的对象。如果 属性 返回的值可以在初始化时设置,那么 是极好的。
它说:
Property or indexer 'IFoo.TestFoo' cannot be assigned to -- it is read only.
有没有办法解决这个问题,或者我是否仍然必须在这种情况下使用带有支持字段的属性?
我正在使用 Visual Studio 2015 RC 和 Visual Studio Code 0.3.0 with DNX451 1.0.0-beta4.
我有raised an issue over at the Roslyn GitHub page.
是一个关于接口定义的问题,正则属性可以读取。我的问题是关于使用新的 C# 6 功能显式实现这样的接口,从理论上讲,这应该使之成为可能。请参阅我在第一句中链接的另一个类似问题(但对于 C# 5,其中 getter-only auto-properties where not available)。
我想你想要这个
string IFoo.TestFoo { get; } = "World";
您可以通过为显式实现的 属性 使用只读支持字段来解决这个问题。您可以将注入的值分配给构造函数中的支持字段,显式 属性 的 get 实现将 return 它。
public class Impl : IFoo
{
private readonly string _testFoo;
string IFoo.TestFoo => _testFoo;
public Impl(string value)
{
_testFoo = value;
}
}
为显式接口实现使用自动属性was not possible in C# 5, but now that C# 6 supports getter-only auto-properties,现在应该可以了吧?
在 C# 6 中创建自动 属性 成功,但是当试图在构造函数中为其赋值时,您必须先将 this
转换为接口类型,因为实现是明确的。但这就是 VS 2015 RC 和 VS Code 0.3.0 显示可以在评论中看到的错误的地方:
using static System.Console;
namespace ConsoleApp
{
public interface IFoo { string TestFoo { get; } }
public class Impl : IFoo
{
// This was not possible before, but now works.
string IFoo.TestFoo { get; }
public Impl(string value)
{
// ERROR: Property or indexer 'IFoo.TestFoo' cannot be assigned to -- it is read only.
((IFoo)this).TestFoo = value;
}
}
public class Program
{
// Yes, not static. DNX supports that (for constructor DI).
public void Main(string[] args)
{
IFoo foo = new Impl("World");
WriteLine($"Hello {foo.TestFoo}");
ReadKey(true);
}
}
}
注意:我将设置常量值的原始问题更新为 TestFoo
。在我的真实场景中,值来自注入到构造函数中的对象。如果 属性 返回的值可以在初始化时设置,那么
它说:
Property or indexer 'IFoo.TestFoo' cannot be assigned to -- it is read only.
有没有办法解决这个问题,或者我是否仍然必须在这种情况下使用带有支持字段的属性?
我正在使用 Visual Studio 2015 RC 和 Visual Studio Code 0.3.0 with DNX451 1.0.0-beta4.
我有raised an issue over at the Roslyn GitHub page.
我想你想要这个
string IFoo.TestFoo { get; } = "World";
您可以通过为显式实现的 属性 使用只读支持字段来解决这个问题。您可以将注入的值分配给构造函数中的支持字段,显式 属性 的 get 实现将 return 它。
public class Impl : IFoo
{
private readonly string _testFoo;
string IFoo.TestFoo => _testFoo;
public Impl(string value)
{
_testFoo = value;
}
}