修改表达式<Func<object>>

Modifying Expression<Func<object>>

我需要修改给定的表达式。

默认表达式将这样调用:

LocalizationService.Current.GetString(() => GeneralResources.Products)

其中GeneralResources.Products是静态字符串:

public class GeneralResources
{
        public static string Products => "Products";
        public static string ProductsExtra => "Products extra";
}

现在我添加了一种方法来检查特定内容以更改表达式,因此不会使用 GeneralResources.Products,而是使用 GeneralResources.Products额外

但我不知道如何准确地改变这个对象。

这是我目前的情况:

public static string GetString(
    this LocalizationService service,
    Expression<Func<object>> resource,
    params object[] formatArguments)
{
    if (customCheck == true)
    {
        // TODO: Change from GeneralResources.Products (resource) to GeneralResources.ProductsExtra
        
        var translation = service.GetStringByCulture(() => resource + "Extra", CultureInfo.CurrentUICulture, formatArguments);
        if (!string.IsNullOrEmpty(translation)) return translation;
    }

    return service.GetStringByCulture(resource, CultureInfo.CurrentUICulture, formatArguments);
}

希望有人能帮助我。

提前致谢!

格茨桑德

终于用这段代码修复了它:

public static string GetString(
        this LocalizationService service,
        Expression<Func<object>> resource,
        params object[] formatArguments)
    {
        if (resource == null)
            throw new ArgumentNullException(nameof(resource));

        if (customCheck == true)
        {
            try
            {
                var body = resource.Body as MemberExpression;
                MemberExpression member = Expression.Property(null, body.GetDeclaringType(), body.Member.Name + "Extra");
                resource = Expression.Lambda<Func<object>>(member);

                var translation = service.GetStringByCulture(resource, CultureInfo.CurrentUICulture, formatArguments);
                if (!string.IsNullOrEmpty(translation)) return translation;
            }
            catch
            {
                //ignore exception, means that translation isn't available
            }
        }

        return service.GetStringByCulture(resource, CultureInfo.CurrentUICulture, formatArguments);
    }