Domanda

I have created property from code with ReSharper (moved from some method that was too long):

private static SomeFunctions XmlSomeFunctions
{
    get
    {
        // some logic
        return someFunctions;
    }
}

However, I want it to be something like this:

private static SomeFunctions xmlSomeFunctions;
private static SomeFunctions XmlSomeFunctions
{
    get
    {
        if (xmlSomeFunctions == null)
        { 
            // some logic
            xmlSomeFunctions = someFunctions;
        }
        return xmlSomeFunctions;
    }
}

But I have not found any entry in context menu (Ctrl+Shift+R = Refactor This) in ReSharper that could help me with this task. Is there any way I can create above code automatically with ReSharper?

If I won't rewrite this code (manually for now, preferably with ReSharper, if I know how), I will have that logic executed many times (instead of once) if I ask for XmlSomeFunctions in different places in my code.

È stato utile?

Soluzione 2

You do have this possibility. Place your cursor on the name of the property (XmlSomeFunctions) and click on the hammer icon to the left.

enter image description here

You'll have to add the if statement yourself though.

Altri suggerimenti

What your actually trying to do is create a lazily instantiated property. A better way to do this is just use the Lazy class in .Net. Reuse this class instead of trying to automate the repetitive code with resharper would be my advice.

See http://msdn.microsoft.com/en-us/library/dd642331(v=vs.110).aspx

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top