msb*_*uva 0 c# parameters properties dynamic
我们有一个具有getter的类的属性 - 返回值
例如:
Public Employee Department
{
get { return GetTheValue("Mid Level") ;}
}
Run Code Online (Sandbox Code Playgroud)
GetTheValue是我们的框架方法,它接受参数值并返回字符串.参数vlaue现在是硬编码的,我们想要使其动态化...我们希望将该参数值直接传递给Property.我们可以将参数传递给属性吗?不想把它作为方法,只想将参数传递给属性..我怎么能实现这个呢?
你可以使用类似于属性的索引器 ......
public Employee this[string department]
{
get
{
return GetTheValue(department);
}
}
...
var instance = new Whatever();
var employee = instance["Mid Level"]
Run Code Online (Sandbox Code Playgroud)
但那是滥用.我只想用一种方法......
public Employee GetDepartment(string department)
{
return GetTheValue(department);
}
Run Code Online (Sandbox Code Playgroud)