简体   繁体   中英

Shortcut to instantiate an object in Visual Studio

I have a class with more than 8 properties, and I often want to instantiate it in some method, but it is super tedious to have to write the properties one by one to assign them the value.

Is there any way to insert a kind of "code fragment" with a keyboard shortcut, which allows me to insert the instance of the class, and I just modify the values ​​to add?.

I do not want to use constructors because I want the instance to be readable for the reader of my code and because the constructors do not work in LinQ to SQL.

I Use Visual Studio 2015, C#, Resharper.

Thank you very much.

If you are initialising like this:

  var example = new Example()
    {
        ...
    };

Then you can use Ctrl-Space and it will keep offering you properties left for auto complete that you have yet to set.

I've created the Generate an initializer for a new object with names of public properties and fields command for the Visual Commander extension. Call it after entering the class name and it will generate an initializer:

在此输入图像描述

As far as I know, there's not a built in way to generate an instantiator with all the properties of the object. Generally when you have to do this, you would go through a constructor so you know that the object is getting created correctly.

You could create some type of code snippet in Visual Studio, but you'd have to make it yourself and it would only really work for that object....

You can view this post as well since it's pretty close to what you're looking for but it doesn't sound like they found a great way to do this either: Is there a way, at design time, to initialize an object with all properties in Visual Studio 2010?

You can create custom code snippet the format is like following:

<?xml version="1.0" encoding="utf-8" ?> <CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <Header> <Title>Create instance of my class</Title> <Author>Author</Author> <Shortcut>ShortCut (then press tab tab)</Shortcut> <-- Put your snippet <Description>Description</Description> <SnippetTypes> <SnippetType>SurroundsWith</SnippetType> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal> <ID>message</ID> <Default>my function</Default> </Literal> </Declarations> <Code Language="CSharp"> <![CDATA[ var myclass = new MyClass() { Property1 = 1, Property2 = 2 }; ]]> </Code> </Snippet> </CodeSnippet>

You can save this in Nodepad and the you have to save this file with .snippet extension. Look for folder ..Visual Studio 2017\\Code Snippets\\Visual C#\\My Code Snippets
When you are ready open VS and go in Tools -> Code Snippet Manager and choose Language CSharp then select your snippet from My Code Snippets folder and you should be ready to use it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM