添加二传手继承的只读属性在C#接口 [英] Adding setter to inherited read-only property in C# interface

查看:237
本文介绍了添加二传手继承的只读属性在C#接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须声明某些属性的接口(简称为编号只中的例子)只用 GET 方法。实现此接口的类没有提供该物业的公共setter方法​​:

 公共接口IMetadataColumns 
{
的Int32标识{搞定; }
}

现在我需要具有相同属性的另一个接口,但现在他们必须全部可写为好。所以我增加了一个新的接口,从旧的,每个属性也有一个setter继承:

  public接口IMetadataColumnsWritable:IMetadataColumns 
{
的Int32标识{搞定;组; }
}



的Visual Studio现在强调这一点,并警告我使用关键字,如果隐藏了旧物业之意。






我该怎么现在要做的?我有实施 IMetadataColumns 接口,它需要一些属性为只读类,但我也有其他类到底在哪那些相同的属性必须是可写为好。



我猜隐藏属性听起来有点不喜欢去...


解决方案

在接口都参与其中,在关键字并不意味着你的隐藏的在同样的意义与阶级属性。



您类实现 IMetadataColumnsWritable 只会有一个编号 ,不管你是否将它转换为基 IMetadataColumns 接口或不(除非的添加为只读属性明确的实现 - 但这不是。需要并且只允许在这种情况下,错误)



在换句话说,你可能有:

  //这个接口是公开的,它可以让大家阅读的Id 
公共接口IMetadataColumns
{
INT标识{搞定; }
}

//这个人是可写的,但它的内部
内部接口IMetadataColumnsWritable:IMetadataColumns
{
//我们需要使用新在这里,但是这并不意味着
//你最终将有两个干将
新INT标识{搞定;组; }
}

//内部实现是可写的,但你可以通过它
//其他组件通过只读IMetadataColumns
//接口
内部类MetadataColumns:IMetadataColumnsWritable
{
公众诠释标识{搞定;组; }
}



所以,除非你明确地实现只读属性作为一个单独的一个,你将只有一个属性,两者的接口用来访问它:

  VAR列=新MetadataColumns {n = 5}; 

变种X =(列作为IMetadataColumns).ID;
变种Y =(列作为IMetadataColumnsWritable).ID;

Debug.Assert的(X == Y);



使用隐藏的成员在类中的关键字,在另一方面,就是我的不可以建议在大多数情况下,因为它基本上创建了一个全新的成员,这恰好被称为一样,在基类中不同成员


I have an interface that declares some properties (shortened to Id only in the example) with only a get method. Classes implementing this interface do not have to provide a public setter for this property:

public interface IMetadataColumns
{
    Int32 Id { get; }
}

Now I need another interface with the same properties, but now they must all be writable as well. So I added a new interface inheriting from the old one where each property also has a setter:

public interface IMetadataColumnsWritable : IMetadataColumns
{
    Int32 Id { get; set; }
}

Visual Studio now underlines this and warns me to use the new keyword if hiding the old properties was intended.


What shall I do now? I have classes implementing the IMetadataColumns interface which need some of the properties to be read-only, but I also have other classes where exactly those same properties must be writable as well.

I guess hiding a property sounds somehow not like the way to go...

解决方案

When interfaces are involved, the new keyword doesn't mean you are hiding the property in the same sense as with classes.

Your class implementing IMetadataColumnsWritable will only have one Id, regardless of whether you cast it to the base IMetadataColumns interface or not (unless you add an explicit implementation for the readonly property - but this is not needed and would only allow for errors in this case).

In other words, you might have:

// this interface is public, and it allows everyone to read the Id
public interface IMetadataColumns
{
    int Id { get; }
}

// this one is writeable, but it's internal
internal interface IMetadataColumnsWritable : IMetadataColumns
{
    // we need to use 'new' here, but this doesn't mean
    // you will end up with two getters
    new int Id { get; set; }
}

// internal implementation is writeable, but you can pass it 
// to other assemblies through the readonly IMetadataColumns 
// interface
internal class MetadataColumns : IMetadataColumnsWritable
{
    public int Id { get; set; }
}

So, unless you explicitly implement the readonly property as a separate one, you will have only a single property, whichever interface you use to access it:

var columns = new MetadataColumns { Id = 5 };

var x = (columns as IMetadataColumns).Id;
var y = (columns as IMetadataColumnsWritable).Id;

Debug.Assert(x == y);

Hiding a member inside the class using the new keyword, on the other hand, is what I would not recommend in most cases, because it basically creates a completely new member, which just happens to be called the same as a different member in the base class.

这篇关于添加二传手继承的只读属性在C#接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆