I need to find property inside of generic type. This is an old way (and since my code is dedicated for WinRT I believe I need another approach):
PropertyInfo pi = typeof(TRp).GenericTypeArguments[0].GetProperty(idField, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
I need to achieve the same result using GetRuntimeProperties
. This is my approach:
PropertyInfo pi = typeof(TRp).GenericTypeArguments[0].GetRuntimeProperties().Single(p => p.Name.ToUpper() == idField.ToUpper()...
as you can see I implemented IgnoreCase
in a custom way, probably it can be done better?
How can I implement remaining BindingFlags
?
Thank you!
This is how Type.GetRuntimeProperties is implemented: public static IEnumerable<PropertyInfo> GetRuntimeProperties (this Type type) { CheckAndThrow (type); IEnumerable<PropertyInfo> properties = type.GetProperties (everything); return properties; }
The simpler of the two getProperty methods takes a single argument. getProperties: The java.lang.System.getProperties () method determines the current system properties. getProperty (String key) : java.lang.System.getProperty (String key) method returns a string containing the value of the property.
Return Value: This method returns an array of PropertyInfo objects representing all public properties of the current Type or an empty array of type PropertyInfo if the current Type does not have public properties. Below programs illustrate the use of Type.GetProperties () Method: Console.Write ("name is null."); Console.Write ("name is null.");
If the property does not exist, this version of getProperty returns null. This is based on key – value pair as mentioned in the table given below. public static String getProperty (String key) Parameters : key : key whose system property we want Returns : System property as specified the key Null : if there is no property present with that key.
You actually dont need to. This is how Type.GetRuntimeProperties
is implemented:
public static IEnumerable<PropertyInfo> GetRuntimeProperties(this Type type)
{
CheckAndThrow(type);
IEnumerable<PropertyInfo> properties = type.GetProperties(everything);
return properties;
}
Where everything
is defined as following:
private const BindingFlags everything = BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static;
Which means it will already look for your required flags.
Edit:
If you want to specify BindingFlags
yourself, you can write your own custom extension method:
public static class TypeExtensions
{
public static IEnumerable<PropertyInfo> GetAllProperties(this TypeInfo type,
BindingFlags bindingFlags)
{
var propertyInfos = type.GetProperties(bindingFlags);
var subtype = type.BaseType;
if (subtype != null)
list.AddRange(subtype.GetTypeInfo().GetAllProperties(bindingFlags));
return propertyInfos.ToArray();
}
}
Note this hasn't been tested. It is merely an attempt to show you that you can do it yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With