0

Let's say I have School model

public class School {

public string Name {get; set;} // property

}

I have a array list of Properties coming from database

var arraylist = ["ComputerParts","CellphoneParts"]

is there a way that can I add new property in School Model using for loop?

this what i'm thinking

  1. for loop arraylist
  2. Add new property in school model
//add this -> public string ComputerParts{get; set;} -> in school model
// add this -> public string CellphoneParts {get; set;} -> in school model 

Here's the expected result

public class School {

public string Name {get; set;} 
public string ComputerParts{get; set;}
public string CellphoneParts {get; set;}

}
Filburt
  • 17,626
  • 12
  • 64
  • 115
Marc Kenneth Lomio
  • 429
  • 1
  • 4
  • 11
  • 1
    are you building a code generator or something? Otherwise, I don't see how the question really makes sense. – ADyson Oct 12 '20 at 09:35
  • 3
    Sounds like XY Problem. – Filburt Oct 12 '20 at 09:37
  • You need tell us what's you want to do, a code generator or database migration ? – Yiao SUN Oct 12 '20 at 09:37
  • 1
    sure there is a way - [dynamic](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/walkthrough-creating-and-using-dynamic-objects) or [reflection + subclass](https://stackoverflow.com/a/14724876/2716623). But this is tricky and hard to maintain (reflection + subclass way). You should avoid it at all cost! – vasily.sib Oct 12 '20 at 09:42
  • i have a report generations task that has dynamic columns, let's say i have top 5 data the top 5 data would be the columns, and i'm using sql dapper to get the data something like this QueryAsync my concern is how would i know which properties or columns will show in DTO or model? – Marc Kenneth Lomio Oct 12 '20 at 09:42
  • 2
    `Dictionary ExtraColumns { get; }` ? – vasily.sib Oct 12 '20 at 09:46
  • i think that would work, thanks! – Marc Kenneth Lomio Oct 12 '20 at 09:48
  • @drmkc you should add that detail to the question (using your question's "edit" button), as it's quite fundamental to the context of what you're asking. – ADyson Oct 12 '20 at 09:52
  • @vasily.sib you should probably make that an answer (with a bit of context added) – ADyson Oct 12 '20 at 09:52

1 Answers1

2

Looks like you are looking for a way to transfer some dynamic set of key-value pairs over network with your DTO. Instead of dynamicly adding properties to some class (which is actually also possible, but a lot more tricky to implement), I would suggest you to use a simple Dictionary<string, string> or even Dictionary<string, object>:

public class SchoolDTO {
    public string Name { get; set; } 
    public Dictionary<string, string> ExtraColumns { get; set; }
        = new Dictionary<string, string>();
}

var schoolDTO = new SchoolDTO
{
    Name = "Sample school",
    ExtraColumns =
    {
        { "ComputerParts", "part1,part2,part3" },
        { "CellphoneParts", "part1,part2,part3" }
    }
};
vasily.sib
  • 3,871
  • 2
  • 23
  • 26