给出如下表达式:
m => m.Employee.Address.Line1
我需要创建一个函数,它可以遍历表达式,拉出所涉及的对象,或者至少链中的最终对象(即上面的Address对象)。因此,对于上面的示例,该函数将找到:
目的是能够为“路径”生成配置信息。
如果我至少可以获得最终的“包含”对象,那么我可以生成类型信息,并且如果它支持特定属性或方法则调用该对象,以从对象(和对象)请求其他配置信息键入以获取容器类型的配置信息。
即使是稍微复杂一点的陈述也会使这更加困难,例如:
m.Employee.Address [I] .Line1
---只是提供我在MVC中为BASIC场景工作的样本。下面的GetContainingModel()在加载的DataAnnotationsMetadataProvider中的CreateMetadata()内部调用。传递给CreateMetadata()的Model是Lambda语句ROOT的模型,而不是我们想要的。所以逻辑试图找到最终包含的对象。我宁愿不使用反射,因为我正在做或更糟,伸手去找私人会员(坏),但是......我很想得到建议。
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
private object GetContainingModel()
{
if (_metadata == null)
{
throw new SystemException("Unexpected NULL Metadata");
}
var model = _metadata.Model;
try
{
if (_modelAccessor != null)
{
var data = _modelAccessor.Target;
if (data != null)
{
if (GetModelFromContainer(data, ref model))
{
GetModelFromExpression(data, ref model);
}
}
}
}
catch (Exception ex)
{
var msg = ex.Message;
}
return model;
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
private bool GetModelFromContainer(object data, ref object model)
{
var isSet = false;
var container = data.GetType().GetField("container");
if (container != null)
{
model = container.GetValue(data);
isSet = true;
}
else
{
var vdi = data.GetType().GetField("vdi");
if (vdi != null)
{
var viewDataInfo = vdi.GetValue(data) as ViewDataInfo;
if (viewDataInfo != null)
{
model = viewDataInfo.Container;
isSet = true;
}
}
}
return isSet && model != null;
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
private bool GetModelFromExpression(object data, ref object model)
{
if (model == null)
return false;
var expressionField = data.GetType().GetField("expression");
if (expressionField == null)
return false;
return GetModelFromExpression(expressionField.GetValue(data) as LambdaExpression, ref model);
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
private bool GetModelFromExpression(LambdaExpression sourceExpression, ref object model)
{
if (sourceExpression == null)
return false;
var expressionBody = sourceExpression.Body as MemberExpression;
if (expressionBody == null || expressionBody.NodeType != ExpressionType.MemberAccess || expressionBody.Member == null)
return false;
var expression = expressionBody.Expression as MemberExpression;
if (expression == null)
{
return false;
}
switch (expression.Member.MemberType)
{
case MemberTypes.Field:
if (expression.NodeType == ExpressionType.MemberAccess)
{
var fieldInfo = (FieldInfo)expression.Member;
if (fieldInfo != null)
{
model = fieldInfo.GetValue(model);
}
}
break;
case MemberTypes.Property:
var propertyInfo = (PropertyInfo)expression.Member;
if (propertyInfo != null)
{
model = propertyInfo.GetValue(model, null);
}
break;
}
return model != null;
}