Search code examples
c#linqreflectionattributes.net-2.0

Convert code that uses Linq to code that does not


The following code snippet returns an array of PropertyInfo objects that have a defined attribute:

var props = t.GetProperties().Where(
                         prop => Attribute.IsDefined(prop, typeof(MyAttribute)));

.
.
How can I do the same thing in .NET2.0, and therefore without using Linq?


Solution

  • try

    List<PropertyInfo> props = new List<PropertyInfo> ();
    
    foreach (PropertyInfo prop in t.GetProperties())
    {
        if ( Attribute.IsDefined(prop, typeof(MyAttribute)) )
             props.Add (prop);
    }