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: GOstrowsky **

Hello,
Thank you for describing us the scenario. A simple change of calling the ___extractPropertyName___ from the __SortableItem__ class would avoid doing the overload you suggest. I believe that the overload method you propose tends to couple the classes and it gets misaligned with the _Single Responsibility principle._

A good alternative of what you could do without the need of overloading the ___extractPropertyName___ method, is to call a new method on __SortableItem__ which it would return the name of their properties. This way, the __SortableItem__ would use the ___extractPropertyName___ method implemented in __PRISM__:


```C#
private void OnSortableItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var changedItem = (SortableItem)sender;

if (e.PropertyName == changedItem.getDirectionPropertyName()) {
GenerateSortIndexes();
}
else if (e.PropertyName == changedItem.getIndexPropertyName()) {
Reindex(changedItem);
}
}
```

```C#
public class SortableItem{

public ListSortDirection? Direction {..}
public int Index {..}

public string getDirectionPropertyName()
{
return PropertySupport.ExtractPropertyName( ( ) => Direction);
}

public string getIndexPropertyName()
{
return PropertySupport.ExtractPropertyName( ( ) => Index);
}
...
}
```

Thanks.


Viewing all articles
Browse latest Browse all 1878

Trending Articles



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