Last active
August 29, 2015 13:57
-
-
Save JohanLarsson/9662344 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using System.Linq.Expressions; | |
| /// <summary> | |
| /// Class provides methods to obtain member names of data types. | |
| /// </summary> | |
| public static class NameOf | |
| { | |
| /// <summary> | |
| /// Returns the name of a property provided as a property expression. | |
| /// </summary> | |
| /// <typeparam name="T">Type of the property.</typeparam> | |
| /// <param name="propertyExpression">Property expression on the the form () => Instance.Property.</param> | |
| /// <returns>Returns the simple name of the property.</returns> | |
| public static string Property<T>(Expression<Func<T>> propertyExpression) | |
| { | |
| var path = PathExpressionVisitor.GetPath(propertyExpression); | |
| if (path.Length == 1) | |
| { | |
| return path.Single().Member.Name; | |
| } | |
| throw new Exception("Trying to get the name of a nested property: " + string.Join(".", path.Select(x => x.Member.Name))); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Common.Helpers; | |
| using NUnit.Framework; | |
| public class NameOfTests | |
| { | |
| public string DummyProperty { get; private set; } | |
| [Test] | |
| public void NameOfPropertyHappyPath() | |
| { | |
| var name = NameOf.Property(() => DummyProperty); | |
| Assert.AreEqual("DummyProperty", name); | |
| } | |
| [Test] | |
| public void ThrowsOnNestedProperty() | |
| { | |
| var exception = Assert.Throws<Exception>(() => NameOf.Property(() => DummyProperty.Length)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Linq.Expressions; | |
| public class PathExpressionVisitor : ExpressionVisitor | |
| { | |
| private readonly List<MemberExpression> _path = new List<MemberExpression>(); | |
| public static MemberExpression[] GetPath<TSource, TResult>(Expression<Func<TSource, TResult>> expression) | |
| { | |
| var visitor = new PathExpressionVisitor(); | |
| visitor.Visit(expression.Body); | |
| return Enumerable.Reverse(visitor._path).ToArray(); | |
| } | |
| public static MemberExpression[] GetPath<T>(Expression<Func<T>> expression) | |
| { | |
| var visitor = new PathExpressionVisitor(); | |
| visitor.Visit(expression.Body); | |
| return Enumerable.Reverse(visitor._path).ToArray(); | |
| } | |
| protected override Expression VisitMember(MemberExpression node) | |
| { | |
| _path.Add(node); | |
| return base.VisitMember(node); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <WrapPanel Grid.Column="1" VerticalAlignment="Center"> | |
| <WrapPanel.Resources> | |
| <ControlTemplate x:Key="MainMenuButtonTemplate" TargetType="{x:Type controls:MainMenuButton}"> | |
| <Grid HorizontalAlignment="Left" Width="250" Height="250" Margin="3"> | |
| <Grid.RowDefinitions> | |
| <RowDefinition Height="200" /> | |
| <RowDefinition Height="50" /> | |
| </Grid.RowDefinitions> | |
| <Border Background="{StaticResource ItemPlaceholderBackgroundThemeBrush}" Name="Border" | |
| BorderThickness="4" | |
| BorderBrush="{StaticResource ItemPlaceholderBackgroundThemeBrush}"> | |
| <Image Source="{TemplateBinding ImageSource}" Stretch="None" /> | |
| </Border> | |
| <StackPanel Margin="0,0,0,0" Grid.Row="1" MinHeight="50" Name="Panel" | |
| VerticalAlignment="Bottom" | |
| Background="{StaticResource ItemOverlayBackgroundThemeBrush}"> | |
| <TextBlock Text="{TemplateBinding DisplayName}" | |
| Foreground="White" | |
| FontSize="16" | |
| Margin="15,5,15,0" /> | |
| <TextBlock Text="{TemplateBinding Description}" | |
| Foreground="Gray" | |
| TextWrapping="NoWrap" | |
| Margin="15,3,15,1" /> | |
| </StackPanel> | |
| </Grid> | |
| <ControlTemplate.Triggers> | |
| <Trigger Property="IsPressed" Value="true"> | |
| <Setter TargetName="Border" | |
| Property="BorderBrush" | |
| Value="{DynamicResource Accent}" /> | |
| </Trigger> | |
| </ControlTemplate.Triggers> | |
| </ControlTemplate> | |
| </WrapPanel.Resources> | |
| <controls:MainMenuButton Template="{StaticResource MainMenuButtonTemplate}" | |
| DisplayName="{Binding OrderNumber}" | |
| ImageSource="{Binding HasOrder, Converter={StaticResource OrderConverter}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" | |
| Command="{Binding MenuCommand}" | |
| Uri="/Map.Client;component/Pages/Order.xaml" /> | |
| </WrapPanel> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment