PropertySuport.ExtractPropertyName is not supported when you want to extract property:
a, from other class then the caller's class
b, from static method/constructor
```
public class ClassA
{
public string Property1 { get; set; }
}
public class ClassB
{
public string Property2 { get; set; }
public string GetClassBPropertyName()
{
return PropertySupport.ExtractPropertyName(() => Property2)
}
public string GetClassAPropertyName()
{
throw new NotImplementedException();
}
}
```
If you add new method to PropertySupprt:
```
public static string ExtractPropertyName<TPropertyType, TClassType>(Expression<Func<TClassType, TClassType, TPropertyType>> propertyExpression)
{
if (propertyExpression == null)
{
throw new ArgumentNullException("propertyExpression");
}
var memberExpression = propertyExpression.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException("The expression is not a member access expression.", "propertyExpression");
}
var property = memberExpression.Member as PropertyInfo;
if (property == null)
{
throw new ArgumentException("The member access expression does not access a property.", "propertyExpression");
}
var getMethod = property.GetGetMethod(true);
if (getMethod.IsStatic)
{
throw new ArgumentException("The referenced property is a static property.", "propertyExpression");
}
return memberExpression.Member.Name;
}
```
then it could be used like this:
```
public string GetClassAPropertyName()
{
PropertySupport.ExtractPropertyName<string, ClassA>((x, y) => x.Property2)
}
```
Comments: ** Comment from web user: DCherubini **
a, from other class then the caller's class
b, from static method/constructor
```
public class ClassA
{
public string Property1 { get; set; }
}
public class ClassB
{
public string Property2 { get; set; }
public string GetClassBPropertyName()
{
return PropertySupport.ExtractPropertyName(() => Property2)
}
public string GetClassAPropertyName()
{
throw new NotImplementedException();
}
}
```
If you add new method to PropertySupprt:
```
public static string ExtractPropertyName<TPropertyType, TClassType>(Expression<Func<TClassType, TClassType, TPropertyType>> propertyExpression)
{
if (propertyExpression == null)
{
throw new ArgumentNullException("propertyExpression");
}
var memberExpression = propertyExpression.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException("The expression is not a member access expression.", "propertyExpression");
}
var property = memberExpression.Member as PropertyInfo;
if (property == null)
{
throw new ArgumentException("The member access expression does not access a property.", "propertyExpression");
}
var getMethod = property.GetGetMethod(true);
if (getMethod.IsStatic)
{
throw new ArgumentException("The referenced property is a static property.", "propertyExpression");
}
return memberExpression.Member.Name;
}
```
then it could be used like this:
```
public string GetClassAPropertyName()
{
PropertySupport.ExtractPropertyName<string, ClassA>((x, y) => x.Property2)
}
```
Comments: ** Comment from web user: DCherubini **
Hi,
Thanks for posting this suggestion.
However, I am not aware of how this change could be useful in Prism.
It could be useful if you could describe a couple of scenarios where this could be used and what kind of problems could be tackled with this in a Prism application.
Thanks,
Damian Cherubini
http://blogs.southworks.net/dcherubini