0

Been looking for a way (either built-in, or through a plugin), to automate the properties and defaults when instantiating a class. For example, if I have the following:

public class MyClass
{
    string MyPropertyString { get; set; }
    int MyPropertyIntWithDefault { get; set; } = 5,
    decimal? MyPropertyDecimalWithNoDefault { get; set; }
}

I'd love to be able to say MyClass MyClassImplemented = new MyClass {, hit a button or click an option, and have it automatically finish my code as:

MyClass MyClassImplemented = new MyClass {
    MyPropertyString = "",
    MyPropertyIntWithDefault = 5,
    MyPropertyDecimalWithNoDefault = null
};

I'm guessing I could write an extension or tool myself using reflection, but if anyone has any suggestions that have already been implemented, I'd love to hear them. Thanks!

* EDIT *

To be clear, what I'm looking for is a way to automate the generation of that stub so that I can then change the ones I want to change, instead of having to either type them in manually or copy-paste them from the definition. I know that I can set default values so that I can generate the class with those values automatically.

Kiran Ramaswamy
  • 605
  • 1
  • 8
  • 19
  • 2
    If that's the defaults you want then why not just have the class initialize them? – Sean Apr 03 '20 at 15:55
  • What I'm looking for is the generation of that stub, so that I can then change the values myself. Right now, what I find myself doing is having to open the class, copy-paste the properties into a commented-out section of where I'm implementing it, and then manually typing in the ones I want to set – Kiran Ramaswamy Apr 03 '20 at 15:58
  • Does this answer your question? [Is there a way, at design time, to initialize an object with all properties in Visual Studio 2010?](https://stackoverflow.com/questions/9083379/is-there-a-way-at-design-time-to-initialize-an-object-with-all-properties-in-v) or [Shortcut to instantiate an object in Visual Studio](https://stackoverflow.com/q/48464576/150605) or [Can you auto-generate code for assigning properties of an object in C#? (Visual Studio)](https://stackoverflow.com/q/32636955/150605) – Lance U. Matthews Apr 08 '20 at 06:22
  • @BACON - the first two are what I'm looking in to. I haven't had the time to work on it yet, though, so I haven't updated this question with an answer. – Kiran Ramaswamy Apr 08 '20 at 18:53

1 Answers1

1

All that you need is just creating a new instance of your class, then this new instance has all of the properties filled by their default value:

MyClass MyClassImplemented = new MyClass();

You just need to change your class a bit to set the properties a default value (don't forget to use ; after properties, however this works in C#6+):

public class MyClass
{
    public string MyPropertyString { get; set; } = "";
    public int MyPropertyIntWithDefault { get; set; } = 5;
    public decimal? MyPropertyDecimalWithNoDefault { get; set; }
}

Based on your update, it seems what you are looking for, is a way to create a code snippet, so that you would be able to append the properties of the class by hitting some button, in this case you can create a code snippet in Visual Studio, you can go through this tutorial https://learn.microsoft.com/en-us/visualstudio/ide/walkthrough-creating-a-code-snippet?view=vs-2019

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • Hey Salah - I'm more looking for a way of simplifying the process of actually setting those values. Right now, what I find myself doing is having to open the class, copy-paste the properties into a commented-out section of where I'm implementing it, and then manually typing in the ones I want to set – Kiran Ramaswamy Apr 03 '20 at 15:59
  • So let's say I have a class that has a dozen properties, and I want to create a new instance of that class with all of the defaults, except I want to set three of them to a non-default value. Right now, I can do that by instantiating my class, and then saying `MyInstance.Prop1 = x;`, `MyInstance.Prop2 = y;`, etc, or inline it with new MyInstance { Prop1 = x, Prop2 = y }, but either way, I need to know the name of the properties first. I want those stubs to be automatically generated, so that I can just set the ones I want instead of having to look them up – Kiran Ramaswamy Apr 03 '20 at 16:06
  • @KiranRamaswamy So what you need in this case is this is Creating a code snippet, please go through this tutorial https://learn.microsoft.com/en-us/visualstudio/ide/walkthrough-creating-a-code-snippet?view=vs-2019 – Salah Akbari Apr 03 '20 at 16:12
  • The code snippet is closer to what I'm going for, yes - but my objective is not to define a snippet for a particular class, but to have that snippet be automatically generated for me, based on the definition of the class, when I click/press a button after typing `new MyClass {` – Kiran Ramaswamy Apr 03 '20 at 16:17
  • @KiranRamaswamy If you are talking about a more generic way, you may want to use as many snippets as you need, otherwise you may want to refactor your code and use the Generics concept in .NET, then generate the snippet for that Generic class. – Salah Akbari Apr 03 '20 at 16:19
  • @KiranRamaswamy You basically need a code snippet which also has Reflection capabilities - detecting all properties of a class and their types - however, code snippets are not that powerful and I'm not sure if there is any other alternative.. – Hakan Yildizhan Apr 03 '20 at 16:30
  • Hey @Salah - haven't found an answer just yet. Looking into an option I found on another question, if it ends up working for me, I'll reference it. – Kiran Ramaswamy Apr 06 '20 at 16:44