[英]get method for a readonly member in C#
I have a class definition in which a readonly member is defined. 我有一个类定义,其中定义了一个只读成员。
private readonly Dictionary<string, string> map = new Dictionary<string, string>();
Now in order to test my design, I want to access this member outside its class definition. 现在,为了测试我的设计,我想在其类定义之外访问此成员。 I was thinking of providing a
get
method but unable to write an error free syntax. 我当时想提供一种
get
方法,但无法编写无错误的语法。
Is it possible to assign value to a member(using new
) and still able to define its get
method? 是否可以将值分配给成员(使用
new
)并且仍然能够定义其get
方法?
PS: I am new to C# language. PS:我是C#语言的新手。
EDIT: I have not written the code, its just a statement I have copied from an already written module. 编辑:我还没有编写代码,它只是我从已经编写的模块中复制的一条语句。 I have made some design changes in the module and want to test it with minimal changes possible in the code, so for that I was looking to get the readonly access of this member outside the class.
我已经在模块中进行了一些设计更改,并希望以最小的代码更改来对其进行测试,因此,我一直在寻求在类之外获取该成员的只读访问权限。
You can define a read-only property for permitting public access to your field: 您可以定义一个只读属性,以允许公共访问您的字段:
private readonly Dictionary<string, string> map =
new Dictionary<string, string>();
public Dictionary<string, string> Map
{
get { return map; }
}
Note that this will only prevent external classes from changing the instance reference assigned to map
, not from changing the content of the dictionary itself. 请注意,这只会阻止外部类更改分配给
map
的实例引用,而不会更改字典本身的内容。
One could argue that you shouldn't write tests for private members of a class. 有人可能会辩称,您不应该为课程的私人成员编写测试。 Tests should only use the public interface and don't rely on the internals of the class, since you should be able to refactor internals of the class without breaking any tests.
测试应该仅使用公共接口,而不应该依赖于类的内部,因为您应该能够重构类的内部而不会破坏任何测试。
If you add a public getter 'only for testing' there's no guarantee that someone will start using somewhere in the project. 如果添加“仅用于测试”的公共获取器,则不能保证有人会开始在项目中的某处使用。
If you really want to expose the dictionary and use .NET 4.5, use ReadOnlyDictionary
class to make sure that the caller won't change anything. 如果您确实想公开该词典并使用.NET 4.5,请使用
ReadOnlyDictionary
类来确保调用者不会进行任何更改。
public IDictionary<string, string> Map
{
get { return new ReadOnlyDictionary<string, string>(map); }
}
Just create a simple getter 只需创建一个简单的吸气剂
public Dictionary<string, string> Mapping
{
get { return map; }
}
You mean something like this? 你的意思是这样的吗?
readonly Dictionary<string, string> _map = new Dictionary<string, string>();
public Dictionary<string, string> Map
{
get { return _map; }
}
Why are you making this variable readonly
? 为什么要将此变量设置为
readonly
?
If you're trying to give access to the values contained in the dictionary you could create a method that exposes the dictionary without allowing it to be modified: 如果您尝试授予字典中包含的值的访问权限,则可以创建一个公开字典而不允许对其进行修改的方法:
public string GetMapValue(string key)
{
return _map[key];
}
You could write a public
property function to return the private readonly map
, eg: 您可以编写一个
public
属性函数以返回private readonly map
,例如:
public Dictionary<string, string> Map { { get { return map; } } }
However, the Dictionary
is still mutable. 但是,该
Dictionary
仍然是可变的。 If you want a read-only Dictionary
, see this SO question . 如果您需要只读
Dictionary
,请参阅此SO问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.