Good day, I am making a list in C# in which I want Array()
another list to be generated with the attributes of Array()
using a for
. This is what I have, but I don't know how I can assign values to the dynamic property that has just been created.
string[] propiedades = { "foo","x"};
var prueba = new List<string>();
for(int x = 0;x <= propiedades;x++){
prueba.Add(propiedades[x]);
prueba.propiedades[x]="test";
}
I'm looking for something like this:
prueba.foo: "test"
prueba.x:"x"
I don't know if what I want to do is possible, thank you very much in advance
Greetings.
It's not directly possible, but the closest thing is a dictionary :
This is how you can consume it:
You could evaluate using the
tipos dinamicos
c# onesUsing Dynamic Type (C# Programming Guide)
Tutorial: Create and use dynamic objects (C# and Visual Basic)
It may not be easy to understand at first, but I think it solves the case you raise. Although you have to take into account that you will not have intellisense in the code with the dynamic properties
I particularly recommend analyzing the class
DynamicObject Class
with this you can create the class you are looking for as explained here
Dynamically Add C# Properties at Runtime
see how it defines the class
to use it
after loading the dictionary of properties and values you can access these directly as if the key were a property
You can't do it because you didn't understand string, string is a string, you can have an array of strings or even a list of strings, or a list of arrays of strings, etc..., but I suspect it's not what you want
I think what you want to do is have a dynamic class to which you add properties or attributes, if that is the case, check this link, here is the answer: https://stackoverflow.com/questions/44067241/add-properties-dynamically- at-run-time-in-existing-class-c-sharp