我想创建一种方法来回应这种情况:
this.GetRessource<T>(p => new { expression = "TestConstant", p.Property });
GetRessource方法必须提取常量值和属性名称。我找到了如何提取属性名称,但我找不到如何获取常量值。
有我的方法。
public void GetResource<TObject>(Expression<Func<TObject, object>> expression)
{
string constantValue = string.Empty;
string propertyName = string.Empty;
// Get propertyName.
var expressionProperties = expression.Body.Type.GetProperties();
var properties = typeof (TObject).GetProperties()
.Where(pO => lExpressionProperties.Any(pE => pO.Name == pE.Name))
.Select(p => p.Name).ToArray();
propertyName = lProperties[0]; // = Property
// constantValue = ?? ; // must return TestConstant
}
我称之为
ParameterExpression parameterExpression = Expression.Parameter(typeof(string), "p");
var constantExpression = Expression.Constant(this.Id, typeof(string));
var expression = Expression.Lambda<Func<string, object>>(constantExpression, parameterExpression).Compile();
this.GetRessource<T>(p => new { expression, p.Property });
有人已经处于这种情况吗?可能吗 ?你能救我吗?
感谢。