Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetRuntimeProperties instead of GetProperty

Tags:

c#

reflection

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!

like image 523
Jacek Wojcik Avatar asked Jan 12 '15 12:01

Jacek Wojcik


People also ask

How to implement getruntimeproperties method in Java?

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; }

What is the difference between getProperty () and getproperties () methods?

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.

What is the use of type getproperties () and type return value?

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.");

Why does getProperty return null when system property does not exist?

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.


1 Answers

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.

like image 76
Yuval Itzchakov Avatar answered Sep 30 '22 08:09

Yuval Itzchakov