简体   繁体   English

使用接口添加私有设置器

[英]Using Interface to add a private setter

I have an Interface that has only one property like below 我有一个仅具有以下属性的接口

public interface IExtendable
{
      Hashtable ExtendedProperties { get; set;}
}

I want setter to be private but it doesnt allow because its coming from an Interface and everyhing should be public in it as I know. 我希望设置器是私有的,但不允许这样做,因为据我所知,它来自接口,并且所有内容都应是公开的。

So what can be the best practice to allowing private setter ? 那么允许私人二传手的最佳实践是什么?

  • Dont use Interface in this scenario or what ? 不要在这种情况下使用Interface还是什么?

Thanks in advance, 提前致谢,

You can not add a private setter to an interface. 您无法将私有设置器添加到接口。 If you want to have a private setter, then your interface will need to look like this: 如果您想要一个私有的setter,那么您的界面将需要如下所示:

public interface IExtendable 
{       
     Hashtable ExtendedProperties { get; } 
} 

And in the implementation of the interface you can add your private setter. 在接口的实现中,您可以添加您的私有设置器。

private interface IExtendable
{
    Hashtable ExtendedProperties { get; }
}

private class Extendable: IExtendable
{
    Hashtable ExtendedProperties { get; private set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM