If you access this property
anObject.Address = "An address Value"
The code will be very "static". He will always have access to Address
You can create more dynamic code like this
void SetProperty (string propertyName, string value) { anObject.Properties[propertyName] = value; }
You would do this if you did not know at compile time which property would be available.
In C #, you usually use Dictionary<TKey,TValue> to store key / value pairs. Automatic access to properties through KVC, as in Objective-C, is not supported in C #. You either declare the property as
public Dictionary<string,string> Properties { get; private set; }
and create an instance in the constructor of the class with
Properties = new Dictionary<string,string>();
then you can access it as follows
anObject.Properties[propertyName] = value;
Or you will need to use Reflection to access the property
Type type = anObject.GetType(); // Or Type type = typeof(TypeOfAnObject); PropertyInfo prop = type.GetProperty(propertyName); prop.SetValue(anObject, propertyValue, null);
However, this is not very effective.
Olivier Jacot-Descombes
source share