1

I already have code working that iterate over class properties The thing is i want my class properties to be determined on the fly. I want to achieve something like that

MyClass
{
   public double value1{ set; get; }
   public double value2{ set; get; }
   public double value3{ set; get; }
    .....
}
 Dictionary<int, MyClass> myDict;
  /*this could be changed idk*/
PropertyInfo variableproperty= myClassesProperties;
foreach (PropertyInfo myClassesProperties in myDict.ElementAt(0).Value.GetType().GetProperties())
{
    for (int i = 0; i < myDict.Count(); i++)
    {
        myDict.ElementAt(i).Value.variableproperty=.......
    }
}

how to write a code to produce this something like that

Update: in the end I want to loop through the values that exist in MyClass manipulating one single specific property each time and get their data.

ma1169
  • 659
  • 1
  • 8
  • 26
  • I am more or less clueless about what you want... Please expand your question with the following information: The properties of which type(s) or object(s) you want to iterate over? What is that MyClass type (the one you are using in your dictionary) looking like? What precisely do you want to obtain: the names of properties, the type of properties and/or the value of properties? –  Apr 29 '17 at 19:51
  • Okay give me a min. – ma1169 Apr 29 '17 at 19:52
  • if defining your class properties at `RunTime` is what you want, try reading up about `ExpandoObject` and the `dynamic` and see if it can help you... also check this link http://stackoverflow.com/q/1653046/3956100 – Niklas Apr 29 '17 at 20:05
  • @Niklas I don't want to define properties, I want to loop through a predefined property that will be determined through a loop – ma1169 Apr 29 '17 at 20:10
  • @elgonzo because i have large database and I don't want to make 50 loops. each loop for a specific property i want to make something dynamic where i can just loop over a random class's properties and inside of it i then loop through the dictionary and return the the value of this property and print the result – ma1169 Apr 29 '17 at 20:22
  • Don't use `.ElementAt(` with a dictionary, it will not work reliably, if you need to reference by index use a `List>` instead. – Scott Chamberlain Jun 05 '17 at 16:40

1 Answers1

1

I'm assuming you want to set the value of a property on an object that is strongly-typed, i.e. you have defined the properties at compile time instead of using a dynamic object like ExpandoObject, and that the name of the targeted property is something that will vary at run-time.

If that's the case, and you have correctly obtained a PropertyInfo instance for the relevant property, then use PropertyInfo.SetValue(myObjectInstance, myNewValue).

You can use Type.GetProperty() to get a PropertyInfo object for any given property on your class:

PropertyInfo myProp = typeof(MyClass).GetProperty("MyPropertyName");

With this you can check if it is writable:

if (myProp.CanWrite) { // do stuff... }

And if you want to get the whole set of PropertyInfo objects for every property on your class:

var myProperties = typeof(MyClass).GetProperties();

Each one of these has a Name property so you can loop over them, inspect their types etc.

Daniel Hume
  • 437
  • 4
  • 9
  • I already have the property info and i have list of all properties i need, what i want is to pass this list one by one and in each time i want to return the value of that property using another loop but I want to have a dictionary search that loop through each class and return this single value – ma1169 Apr 29 '17 at 20:26
  • Ah, well then on each pass through your inner loop you just need to use `myPropertyInfo.GetValue(myObject)` – Daniel Hume Apr 29 '17 at 20:28