I'm running into a problem. I'm a VB.net programmer and I'm trying to learn C#. On many VB projects that I have done I've always used a viewModelBase
class where I can notify my properties over my project, when I try to convert the code from vb to C# I'm getting a method name expected
on the line: if (TypeDescriptor.GetProperties(this)(propertyName) == null)
[Conditional("DEBUG"), DebuggerStepThrough()]
public void VerifyPropertyName(string propertyName)
{
if (TypeDescriptor.GetProperties(this)(propertyName) == null)
{
string msg = "Invalid property name: " + propertyName;
if (this.ThrowOnInvalidPropertyName)
{
throw new Exception(msg);
}
else
{
Debug.Fail(msg);
}
}
}
I really can't find any solution for this! Any help?
Thank you
It sounds like you're just missing the fact that indexer syntax in C# is [key]
. I suspect you want:
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
That's first calling the GetProperties
method, to find the PropertyDescriptorCollection
of all the properties of this
... then it's using the indexer of PropertyDescriptorCollection
to access a specific property by name.
You could also use the "Find" function:
if (TypeDescriptor.GetProperties(this).Find(propertyName, false) == null)
Note that this does a case-sensitive find.
Try this:
[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);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.