Quantcast
Channel: patterns & practices: Prism
Viewing all articles
Browse latest Browse all 1878

Commented Unassigned: ExtractPropertyName can be used only for properties in current class [10306]

$
0
0
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 **

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


Viewing all articles
Browse latest Browse all 1878

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>