detecting property getter usage

Hi


Anyone know which IElement I need to look for in order to detect property getter access, e.g.:


var123 = someObject.PropertyA;


There is an IPropertyAssignment element, but the closest I can find it IReferenceExpression. However I can't spot any obvious property on that type with which I can differentiate between method calls and property access.


fyi this is for r# 4.1


Cheers
Lee

6 comments

Hi!

I think that you should look at IInvocationExpression. That's what uncle Reflector tells me

Cheers,
Bartek.

0

InvocationExpression pertains to method invocations. As far as I can tell, the only way to detect property getter usage is:

    public class DaemonStageProcess : CSharpDaemonStageProcessBase
    {
        readonly IResolveContext resolveContext;

        public DaemonStageProcess(IDaemonProcess process) : base(process)
        {
            resolveContext = new ResolveContext(PsiManager.GetInstance(process.Solution));
        }

        public override void ProcessFile(ICSharpFile file)
        {
            file.ProcessDescendants(this);
        }

        public override void VisitExpressionInitializer(IExpressionInitializer expressionInitializerParam)
        {
            base.VisitExpressionInitializer(expressionInitializerParam);

            var references = expressionInitializerParam.Value.GetReferences();

            foreach(var reference in references)
            {
                var resolved = reference.Resolve(resolveContext);
                var propertyDeclaration = resolved.DeclaredElement as IPropertyDeclaration;

                if (propertyDeclaration != null)
                {
                    ....
                }
            }
        }
    }

Although I also need to investigate statements like:

var abc = myClass.Property = "some value";

0

Let me fix your code:
var propertyDeclaration = resolved.DeclaredElement as IPropertyDeclaration;
IS NOT VALID!

Replace it with the following:

var property = resolved.DeclaredElement as IProperty;
if (property != null)
{
var propertyDeclaration = property.GetDeclarations().GetFirstOrDefault()
as IPropertyDeclaration;
....
}



--
Eugene Pasynkov
ReSharper Technical Lead
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


"Lee Henson" <no_reply@jetbrains.com> wrote in message
news:8778169.32071237296174151.JavaMail.clearspace@app8.labs.intellij.net...

InvocationExpression pertains to method invocations. As far as I can tell,
the only way to detect property getter usage is:

>

public class DaemonStageProcess : CSharpDaemonStageProcessBase
{
readonly IResolveContext resolveContext;

>

public DaemonStageProcess(IDaemonProcess process) : base(process)
{
resolveContext = new
ResolveContext(PsiManager.GetInstance(process.Solution));
}

>

public override void ProcessFile(ICSharpFile file)
{
file.ProcessDescendants(this);
}

>

public override void VisitExpressionInitializer(IExpressionInitializer
expressionInitializerParam)
{
base.VisitExpressionInitializer(expressionInitializerParam);

>

var references = expressionInitializerParam.Value.GetReferences();

>

foreach(var reference in references)
{
var resolved = reference.Resolve(resolveContext);
var propertyDeclaration = resolved.DeclaredElement as
IPropertyDeclaration;

>

if (propertyDeclaration != null)
{
....
}
}
}
}

>

Although I also need to investigate statements like:

>

var abc = myClass.Property = "some value";

>

---
Original message URL:
http://www.jetbrains.net/devnet/message/5233858#5233858



0

In what way is it "NOT VALID!"? It is working. Are there some situations in which I may get a false positive or vice versa?

0

Hello Lee,

You are lucky :) It is just a matter of implementation detail that IProperty
is implemented on the same type as IPropertyDeclaration. It is not the case
in many other situation, and can be changed anytime. DeclaredElement and
Declaration are different things. E.g. partial class has single DeclaredElement
(IClass), but can have two or more declarations (IClassDeclaration).

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


IR> In what way is it "NOT VALID!"? It is working. Are there some
IR> situations in which I may get a false positive or vice versa?
IR>
IR> ---
IR> Original message URL:
IR> http://www.jetbrains.net/devnet/message/5234062#5234062


0

Thanks for the info. I am going to continue to make large numbers of these kinds of errors because I am purely guessing on how to use the openapi based on what I can see via watch windows in vs & reflector. Is there any hope that we might see some documentation (however basic - along the lines of what you just posted) in the near future? Maybe after 4.5 is released? To paraphrase Dr Evil, "throw us a frickin' bone here"!

0

Please sign in to leave a comment.