CodeMan03
CodeMan03

Reputation: 226

C# Possible to auto generate properties based on the known type?

Its 2022. There has to be a way to do this. Lets say I have an empty class object call MyProperties. I have a current strong type variable on the right side of the statement. Is it possible for VS to see this and that the property doesn't exist and auto create the property based on the strong type of the right hand side of the statement?

private class MyProperties 
{
// We are empty so far

}


private void Main()
{
  List<MyProperties> myList = new List<MyProperties>
   {
     Property1 = myStrongTypedClass.AccountNumber,
     Property2 = myStringTypedClass.DoubleList
   // I want it to see that Property 1 or 2 doesn't exist but auto create them based on 
   // the type of AccountNumber and DoubleList.
   }

}

private MyStrongTypedClass

{
  public int AccountNumber {get; set;}
  public decimal Price {get; set;}
  public List<double> DoubleList {get; set}
}

Instead of the compiler complaining that MyProperties does not contain a definition for AccountNumber that it will just auto create it

Upvotes: 1

Views: 52

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

Yes, just write it as if it does exist:

var mp = new MyProperties;

mp.Hello = "Something";

Click on the Hello with the wiggly line, and wait for the lightbulb/spanner button to appear near by, or click the lightbulb/spanner in the margin

Choose "Generate Field" or "Generate Property" as you wish...

enter image description here

Note that it won't work in the example you've put in the question:

List<MyProperties> myList = new List<MyProperties>
{
    Property1 = myStrongTypedClass.AccountNumber,
    Property2 = myStringTypedClass.DoubleList
    // I want it to see that Property 1 or 2 doesn't exist but auto create them based on 
    // the type of AccountNumber and DoubleList.
}

What you've written there is an object initializer that is trying to set properties of List, not a list item initializer trying to add MyProperties items to the List. Trying to set nonexistent Property1 of a List<T> will give you a "doesn't contain a definition for" error but not the option to add it because List is not your class.

Your code will need to look like:

List<MyProperties> myList = new List<MyProperties>
{
    new MyProperties() {
        Property1 = myStrongTypedClass.AccountNumber,
        Property2 = myStringTypedClass.DoubleList
    },
    ...
}

It's a subtle but important difference

enter image description here

Lastly, a few other things we can do in 2022 (or even 2017, I think..) is shorten our newing:

var myList = new List<MyProperties>
{
    new () {
        Property1 = myStrongTypedClass.AccountNumber,
        Property2 = myStringTypedClass.DoubleList
    },
    ...
} 

The compiler knows its a List<MyProperties> on the right, so you don't need to say it on the left, you don't need to put () after the type on the right and you don't need to say MyProperties in the item initializer, but you do need to use new() so C# knows youre trying to make a MyProperties and not an anonymous type

Upvotes: 3

Related Questions