开发者

How to get the variable for which a property is defined in coderush?

开发者 https://www.devze.com 2023-03-27 04:20 出处:网络
privat开发者_如何学运维e int _i; public intCount { get {return _i;} } How to get the variable _i when we have the property \"Count\" using CodeRush APIs.Try the following code, hopefully it will be
privat开发者_如何学运维e int _i;

public int  Count
{
    get {  return _i;  }
}

How to get the variable _i when we have the property "Count" using CodeRush APIs.


Try the following code, hopefully it will be of some help:

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号