所以說我有一個視圖模型稱爲如何通過使用字符串值中的名稱來獲取ViewModel實體?
SampleViewModel
其中包含了3個實體稱爲
entityOne
entityTwo
entityThree
現在我有一個控制器動作的動作,看起來像這樣
public ActionResult TestAction(string Destination)
{
SampleViewModel sampleViewModel = new SampleViewModel();
}
一點點現在我希望能夠根據什麼字符串「目標」設置爲選擇一個特定的實體。 Destination總是被設置爲「entityOne」,「entityTwo」或「entityThree」。所以基本上我想打電話
sampleViewModel.entityOne
如果字符串中包含「entityOne」。
所以在影響我說
sampleViewModel.Destination //Where destination is equal to the one entity in the ViewModel
我怎樣才能做到這一點?
感謝
您可以使用反射來實現此目的。這是一個很好的例子:http://stackoverflow.com/questions/1034826/using-strings-to-select-object-properties –