lundi 14 mars 2022

What is the Type of var in this case?

I stumbled upon this post https://stackoverflow.com/a/5249859/13174465 about reflection in C#. My idea is to create a helper method out of it to use it in multiple places across my code. But I can't figure out what return type the method should have. The IDE shows the type local variable IEnumerable<{PropertyInfo Property, T Attribute}> properties but this isn't accepted as return type of a method.

This is my current code which obviously doesn't work.

public static IEnumerable<PropertyInfo, T> GetPropertiesAndAttributes<T>(object _instance, BindingFlags _bindingFlags = FULL_BINDING) where T : Attribute
    {
        var properties = from p in _instance.GetType().GetProperties(_bindingFlags)
            let attr = p.GetCustomAttributes(typeof(T), true)
            where attr.Length == 1
            select new { Property = p, Attribute = attr.First() as T};

        return properties;
    }

Which return type would be correct to make this method functional?

Thank you!





Aucun commentaire:

Enregistrer un commentaire