Generate code (class) from method and its param as properties in C# using snippets (VS or VSCode)

Viewed 37

I am trying to create a class from given method and use the method param as class properties.

public void MethodName(string param1, string param2)
{
    // ...
}

I need a snippet or any extension that can convert above code as:

public class MethodNameRequest
{
    public string param1 { get; set; }
    public string param2 { get; set; }
}

public void MethodName(MethodNameRequest methodNameRequest)
{
    // ...
}

Any help is appreciated.

Thank you

1 Answers

You could use snippets to achieve a similar effect, see the demo below:

code:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>classProperty</Title>
      <Shortcut>cprop</Shortcut>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[#region
public class $MethodName$Request
{
    public $Type1$ $param1$ { get; set; }
    public $Type2$ $param2$ { get; set; }
}

public void $MethodName$($MethodName$Request methodNameRequest)
{
    // ...
}
#endregion
    ]]>
      </Code>
      <Declarations>
        <Literal>
          <ID>MethodName</ID>
          <ToolTip>The name of the Method.</ToolTip>
          <Default>MethodName</Default>
        </Literal>
        <Literal>
          <ID>Type1</ID>
          <ToolTip>The type of the param1.</ToolTip>
          <Default>string</Default>
        </Literal>
        <Literal>
          <ID>Type2</ID>
          <ToolTip>The type of the param2.</ToolTip>
          <Default>string</Default>
        </Literal>
        <Literal>
          <ID>param1</ID>
          <ToolTip>param1.</ToolTip>
          <Default>param1</Default>
        </Literal>
        <Literal>
          <ID>param2</ID>
          <ToolTip>param2.</ToolTip>
          <Default>param2</Default>
        </Literal>        
      </Declarations>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Output: enter image description here

Related