C# - snippet or template to assign all fields/properties quickly?
Asked Answered
P

1

7

Quick points for someone who might know the answer - is there a snippet or tool that can quickly generate template code to assign all public fields and/or properties of an object?

Example:

public class SomeBloatedClass
{
    public string SomeField1 { get; set; }
    public int SomeField2 { get; set; }
    // etc...
    public string SomeField99 { get; set; }
}

public class TestHarness
{
    public SomeBloatedClass CreateTestObject()
    {
         // Is there a snippet/macro/template that can generate the code to assign
         // all public fields/properties so they can be manually assigned quickly?
         // Something like this...?

         // *Begin auto-generated code
         SomeBloatedClass s = new SomeBloatedClass();
         s.SomeField1 = ;
         s.SomeField2 = ;
         // etc..
         s.SomeField99 = ;
         // *End auto-generated code

         return s;
    }
}

Third-party tools are fine as long as they integrate into Visual Studio.

Edit: I'm just looking to have the tool create empty assignment statements that I could quickly hand-edit with the appropriate values. Ideally, the solution would use the built-in snippet mechanism to navigate from statement to statement via the TAB key - I couldn't represent that clearly using StackOverflow's editor, but if you've used snippets you should know what I mean).

Palatal answered 26/4, 2013 at 20:56 Comment(7)
"assign all public fields and/or properties of an object". With what?Undertenant
Just looking for template code to create assignment statements. The actual values would need be hand-edited. As an example, with VS code snippets, you can tab between values to insert. It would be much, much quicker entering a value and then hitting TAB to move to the next value without having to code out each of the individual assignment statements.Palatal
Not sure if you can auto generate the code but you can give the fields default values then construct the object like this SomeBloatedClass s = new SomeBloatedClass() { SomeField1 ="someData", SomeField2=1,SomeField99="someData"};Undertenant
Yeah, for small classes that works fine. But I'm working with some large classes generated from XML schemas that have up to hundreds of properties. I'm trying to avoid writing out each property by hand. Both from an efficiency perspective and to ensure I don't ever miss a property when working with classes that are that large.Palatal
I seem to remember watching a video on templating objects using T4 templates. Maybe have a look at that. msdn.microsoft.com/en-gb/library/vstudio/bb126445.aspxUndertenant
You've given me a brilliant idea! Instead of using the T4 template to generate the code, use the T4 template to generate a new code snippet with all of the reflected properties, which will then allow me to tab through fields to assign them very quickly. I'm not going to close the question just yet because I'd like to see if there is a more elegant solution, but I've upvoted you for the good suggestion.Palatal
Dan, that's not upvoting :). btw. you can make something like s.SomeField1 = default(typeof(property)); (pseudo) - so you have default - and just enter the ones you want (even w/o tabbing).Impatiens
O
0

There is small snippet or tool that can quickly generate template code to assign all public fields and/or properties of an object?

c# property assigner tool.

and behalf of it we can also generate properties of c# ex.( get set )

c# property generator

Ondine answered 17/8, 2017 at 10:1 Comment(1)
Links not workingTalton

© 2022 - 2024 — McMap. All rights reserved.