我遇到了问题。我是一名VB.net程序员,我正在努力学习C#。在我做过的很多VB项目中,我总是使用viewModelBase
类,我可以在项目中通知我的属性,当我尝试将代码从vb转换为C#时,我得到了{{1}在线:method name expected
if (TypeDescriptor.GetProperties(this)(propertyName) == null)
我真的找不到任何解决方案!有什么帮助吗?
谢谢
答案 0 :(得分:7)
听起来你只是错过了C#中的索引器语法为[key]
这一事实。我怀疑你想要:
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
首先调用GetProperties
方法,找到PropertyDescriptorCollection
所有属性的this
...然后使用{{3} } PropertyDescriptorCollection
按名称访问特定属性。
答案 1 :(得分:0)
您还可以使用“查找”功能:
if (TypeDescriptor.GetProperties(this).Find(propertyName, false) == null)
请注意,这会区分大小写的查找。
答案 2 :(得分:0)
试试这个:
[Conditional("DEBUG"), DebuggerStepThrough()]
public void VerifyPropertyName(string propertyName)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this);
var objValue = properties[propertyName].GetValue(this);
if (objValue == null)
{
string msg = "Invalid property name: " + propertyName;
if (this.ThrowOnInvalidPropertyName)
{
throw new Exception(msg);
}
else
{
Debug.Fail(msg);
}
}
}