0

I need to change the string that the client sends to the values ​​from the class property.

At the moment, it works for me until I get a class that does not support the following entry:

Working example (we start from the payout class):

The customer sends: "Employee.Name" in string

My code:

List <string> parametrs = ClientTxT.Split('.'). ToList();
dynamic MyDynamicObject = payout;
for (int y = 0; y <parametrs.Count; y ++)
    MyDynamicObject = MyDynamicObject [parametrs [y], RowVersion.Current];

I enter the Employee and take the Name property. Everything is OK because the Employee class is compatible with this entry:

object ObjectIRecived = myClass[propertyNameInString, RowVersion];

Example not working: I have a problem when the class is not compatible with this entry, e.g. Number. The Number class has only two property: "Full" and "Simple".

If the customer sends "Number.Full" I got an error:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
  HResult = 0x80131500
  Message = Cannot apply indexing with [] to an expression of type 'Soneta.Core.CoreModule.NumerDokumentuRecord'
  Source = <Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

My guess is that in this case I have to refer directly to Number.Full but I don't know how to do it in the code, since the propertyname is in string

Walczak Jakub
  • 92
  • 1
  • 10
  • Can't you just store the data in a Dictionary? – jason.kaisersmith Jan 11 '20 at 18:04
  • 1
    Does this answer your question? [Get property value from string using reflection in C#](https://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection-in-c-sharp) – Jawad Jan 11 '20 at 18:09
  • Okey, it was Field not a Property for some reason. Your link worked, buyt had to change property to field. – Walczak Jakub Jan 11 '20 at 18:26

0 Answers0