C# Custom Code Snippet Functions

笑着哭i 提交于 2019-12-10 14:36:40

问题


I have a code snippet structures like so:

    private $type$ $lowercaseName$;
    public $type$ $uppercaseName$
    {
        get { return $lowercaseName$; }
        set { $lowercaseName$ = value; }
    }

It generates stuff like:

    private string randomValue;
    public string RandomValue
    {
        get { return randomValue; }
        set { randomValue = value; }
    }

Thats a very oversimplified version... its really a lot more complicated... and its just that much more information to type in. It would be nice if you could type in $uppercaseName$, and then a custom snippet function would assign a value to $lowercaseName$....

But is it even possible to write custom snippet functions? I don't see anything about this in the documentation...

If it is possible... how?


回答1:


It is not yet possible. See this suggestion for 2010 that was denied.

http://connect.microsoft.com/VisualStudio/feedback/details/523601/allow-custom-code-snippet-functions

It seems this feature was suggested when snippets were introduced, and has been re-suggested every version, and shot down saying they don't have enough time.




回答2:


Rather than using the convention of having camelCase fields and PascalCase properties, I have adopted a convention of a using leading underscore for backing fields. The following code snippet works for me.

public $type$ $property$
{
    get { return _$property$;}
    set { _$property$ = value;}
}
private $type$ _$property$;
$end$

And in attempt to snub the "underscore is a prefix and prefixes are bad" holy war, I believ it to be more a convention than a prefix much like upper v. lower initial letters for properties v. parameters. Any use of a field with a leading underscore outside of it's associated property implementation should be code smell.




回答3:


My answer is Resharper. Live Templates.

It is very annoying having this limitation, but like many other Visual Studio limitations, Resharper nails it. It is a performance hog, so I operate with the code analysis turned off. The speed is then acceptable, and live templates save a lot of time.

There are about twenty other good reasons to have a tool LIKE Resharper to make you a fantastic coder.

I can't write "hello world" without Resharper any more... and I can't debug it without Reflector =P

Also, go to the Extension Manager and click Online Gallery. Type "snippet" in the Search box, and there are a few tools there that look like they might help for free :). The extension gallery is pure productivity gold.




回答4:


I also use and recommend the use of the _ prefix for the private variable of properties. Sometimes I wish Microsoft allow scoping of the private variable inside the property, as below to prevent access outside the property.

public string RandomValue
{
    private string randomValue;
    get { return randomValue; }
    set { randomValue = value; }
}


来源:https://stackoverflow.com/questions/5663569/c-sharp-custom-code-snippet-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!