I am using the PropertyInfo method to get the size of an attachment I am uploading using the fyneworks jQuery Multiple File Upload Plugin. It all works fine, however I need to get the real-value of my property. I set the file size of the attachment using the code below:
PropertyInfo sizeProperty = aType.GetProperty("Size");
sizeProperty.SetValue(attachmentItem, fileData.Length, null);
For simplicity say I want to write to the console:
Console.WriteLine("FILE SIZE: " + sizeProperty.ToString());
The above code returns this: FILE SIZE: System.Nullable`1[System.Int32] Size. Which is not really useful.
Side note- although not ideal I have to use reflection as I am restrained by what has been given to me. Ideally I would like to access the properties directly, but I am not able to.
The PropertyInfo
represents attributes and metadata of a property in regards to a type, not an instance. When you want to use it in relation to an instance, you have to use GetValue
or SetValue
In your case, GetValue
will give you the correct size if you pass it the instance, but then @mason question is really interesting: why not access the property of the instance directly?