Question:
In a Xamarin PCL project, is there any way to verify that a property with a given name exists, for a property that is not public?
Context:
In my ViewModelBase
class, the OnPropertyChanged
calls a debug-only method. It helps me finding wrong arguments for OnPropertyChanged in case I could not use the CallerMemberName
logic and had to pass an explicit value for propertyName
:
public abstract class ViewModelBase : INotifyPropertyChanged
{
// ...
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
VerifyPropertyName(propertyName);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
// More code that is the reason why calling OnPropertyChanged can
// make sense even for private or protected properties in my code.
}
[Conditional("DEBUG"), DebuggerStepThrough]
private void VerifyPropertyName(string propertyName)
{
// This only finds public properties
if (GetType().GetRuntimeProperty(propertyName) == null) {
// TODO
// Check if there is a non-public property
// If there is, only print a hint
throw new ArgumentException("Invalid property name: " + propertyName);
}
}
// ...
}
The problem is that GetRuntimeProperty
only finds public properties, i. e. calling OnPropertyChanged
for a private property triggers that exception.
I would like to weaken the condition so that calling OnPropertyChanged
for non-public properties only triggers a Debug.Print
, because my ViewModelBase.OnPropertyChanged
contains more code so that calling OnPropertyChanged
can become beneficial for private or protected properties.
But neither the System.Type
object obtained by GetType()
nor System.Reflection.TypeInfo
obtained through GetType().GetTypeInfo()
as mentioned in this SO answer seem to have methods to find anything else but public properties.