In Xamarin PCL, I'm trying to get the System.Reflection.PropertyInfo of a class I've written so that I can access its properties by their string name to get/set, and Type.GetTypeInfo() is missing, as well as Type.GetProperties. But System.Reflection.PropertyInfo is a valid class. How can I obtain the property info of a class? Do I have to write a wrapper for each platform? (It shows up just fine in the Android/iOS projects).
Asked
Active
Viewed 7,173 times
3 Answers
32
It's an extension, so you need to put
using System.Reflection;
at the top. Then it's available:
TypeInfo typeInfo = this.GetType().GetTypeInfo();
foreach (PropertyInfo propInfo in typeInfo.DeclaredProperties)

user1054922
- 2,101
- 2
- 23
- 37
-
1Just tried this in Profile78. It's still not available. – Dan Vanderboom May 19 '15 at 15:29
-
It doesn't work because `GetProperties()` is excluded from the PCL subset. Although this should indeed enable `GetTypeInfo()`. – Marcin Kaczmarek Nov 24 '15 at 10:51
-
1I've updated my answer to show how I was able to access the object properties by adding using System.Reflection; – user1054922 Nov 25 '15 at 12:35
-
2Maybe that does solve the problem in your particular case, but the `DeclaredProperties` is not a sibstitute for `GetProperties()`. It's because `DeclaredProperties` does not include inherited properties. – Marcin Kaczmarek Nov 28 '15 at 11:12
-
How do I filter to public properties? – Shimmy Weitzhandler May 24 '17 at 12:23
22
I have just run into this, pretty sure the answer is to use:
Type.GetRuntimeProperties

JamesSugrue
- 14,891
- 10
- 61
- 93
-
More relevant information on a similar issue: https://social.msdn.microsoft.com/Forums/en-US/05eebec9-b56f-4b55-9624-dabfc6d24bbd/typeisassignablefrom-in-portable-class-library – Nestor Ledon Sep 28 '15 at 03:33
0
You can also try
using System.Reflection;
Type t = typeof(YOURTYPE);
var properties = t.GetTypeInfo().DeclaredProperties

Max Chu
- 56
- 4