如何获取在coderush中定义属性的变量?

时间:2011-08-10 09:02:53

标签: coderush

private int _i;

public int  Count
{
    get {  return _i;  }
}

使用CodeRush API获取属性“Count”时如何获取variable _i

1 个答案:

答案 0 :(得分:1)

尝试以下代码,希望它会有所帮助:

Variable GetPropertyVariable(Property property)
{
  if (property == null)
    return null;

  Get getter = property.Getter;
  if (getter == null)
    return null;

  Return returnStatement = getter.FindChildByElementType(LanguageElementType.Return) as Return;
  if (returnStatement == null)
    return null;

  Expression returnExpression = returnStatement.Expression;
  ElementReferenceExpression targetExpression = returnExpression as ElementReferenceExpression;
  if (targetExpression == null)
    targetExpression = returnExpression.FindChildByElementType(LanguageElementType.ElementReferenceExpression) as ElementReferenceExpression;
  if (targetExpression == null)
    return null;

  return targetExpression.GetDeclaration(true) as Variable;
}