Change default code snippet in overridden method
Please help me! Can i somehow customize auto-generated code snippet displayed in overridden method body by default?
by default overridden method looks like
public override void Method(int A, object B)
{
base.Method(A, B);
}
i want to replace default code snippet with my own, e.g.
public override void Method(int A, object B)
{
if (A > 0)
{
// some code..
}
else
{
// some code..
}
}
EDIT
i have base class
public class BaseClass
{
public int Result {get;set;}
// This method depends on result
protected virtual void Method() {}
}
there is a lot of classes, that are derived from the BaseClass. All of them have to process Result property in Method() in same way. So, i want to place some code, wich shows how to use it. According to my idea when i type "o开发者_JAVA百科verride" and select "Method()" in intelisense list i get following code:
public class DerivedClass: BaseClass
{
public override void Method()
{
// u have to check result property
if(result > 0)
{
// if result is positive do some logic
}
}
}
instead of default code snippet
public class DerivedClass: BaseClass
{
public override void Method()
{
base.Method();
}
}
FINALLY
Using Template Method pattern is a good idea for such cases.
Thank you all for sharing your thoughts!
In the Code Snippets Manager in visual studio you can modify the MethodOverrideStub.snippet
Getting the sort of behaviour you want where the parameters are used in the snippet will probably tricky - I'm looking into that at the moment but nothing obvious leaps out.
However, just inserting the if/else with some template areas should not be too hard to do.
Well, depending on a code snippet to have people write your methods correctly is not a good idea. If you want that method to always have that structure, you'd be better off using the Template Method pattern:
public abstract class BaseClass
{
// this method forces that structure upon the subclasses
public void Foo()
{
if(result > 0)
{
DoFoo();
}
}
// this is the method that subclasses override
protected abstract void DoFoo();
}
public class DerivedClass : BaseClass
{
public override DoFoo()
{
// now you write the code here
}
}
精彩评论