-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathLocalizationExpressionBuilder.cs
41 lines (35 loc) · 1.59 KB
/
LocalizationExpressionBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (C) 2003-2010 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Created by: Dmitri Maximov
// Created: 2009.12.28
using System.Linq.Expressions;
using System.Reflection;
namespace Xtensive.Orm.Localization
{
internal static class LocalizationExpressionBuilder
{
private static readonly MethodInfo BuildExpressionMethodInfo = typeof(LocalizationExpressionBuilder).GetMethod("BuildExpression", BindingFlags.NonPublic | BindingFlags.Static);
public static LambdaExpression BuildExpression(TypeLocalizationInfo localizationInfo, MemberExpression me, string defaultCultureName)
{
return (LambdaExpression)BuildExpressionMethodInfo
.MakeGenericMethod(localizationInfo.LocalizationTypeInfo.UnderlyingType)
.Invoke(null, new object[] { me.Member.Name, defaultCultureName });
}
// Helper method for building lambda expression in generic way
private static LambdaExpression BuildExpression<TLocalization>(string propertyName, string defaultCultureName)
where TLocalization : Localization
{
Expression<Func<LocalizationSet<TLocalization>, string>> result = set => set
.Where(localization => localization.CultureName == LocalizationContext.Current.CultureName)
.Select(localization => (string)localization[propertyName])
.FirstOrDefault()
??
set
.Where(localization => localization.CultureName == defaultCultureName)
.Select(localization => (string)localization[propertyName])
.FirstOrDefault();
return result;
}
}
}