I am trying to find the first asset of a class which is the integer Is
.. and get its value.
So I've got the following code which always gives false:
foreach (type property type. GetProperties (BindingFlags.Public | BindingFlags.Instance)) { Var is some type = property.PropertyType int; // always wrong. }
Why is this / what I did wrong should be really simple: (
/ I'm having a bad day ...
Change test to:
var firstInt32Property = property.PropertyType == type (Int);
This is necessary because property-type of the property itself is not an integer: it is a System.Type
object Is that which (loosely) shows how property-recipient returns / proportion On the other hand,
loop:
Var firstInt32Property = type.GetProperties (binding flag.public | binding flags.instance). First (p = & gt; p type property == typef (int))
(this An exception will throw if there is no such property present.)
To retrieve the value of the property from an instance containing:
int value = (Int) firstInt32Property.GetValue (myObj, null);
This will definitely fail if 'first' int32
property occurs on an indexer or in fact if there is no passing of it, if such a scenario If you are likely to, you can filter such properties on the original query.
Also keep in mind that this code is of limited use because 'the idea of the first asset of a class which is an integer' is a little bit suspicious:
GetProperties method does not return properties in a specific order, such as alphabetical or announcement order. Your code should not depend on the order in which the properties are returned because this order changes.
Comments
Post a Comment