Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude property from getType().GetProperties()

Hi i'm working in a class library using C#, and i have some classes with some properties.

I just wanna know if i can add something to exclude some properties form the getType().GetProperties().

An example of what i want:

class Test
{
    public string one { get; set; }
    public string two {get ; set;}
}

and if i do this:

static void Main(string[] args)
{

       Test t = new Test();
       Type ty = t.GetType();
       PropertyInfo[] pinfo = ty.GetProperties();

       foreach (PropertyInfo p in pinfo)
       {
           Console.WriteLine(p.Name);
       }
  }

i want the output be something like this:

one

or just one of the properties.

Is possible to do something like that? i don't know if there some kind of modifiers or annotations in C#, that allow me to do what i want.

Thanks.

like image 581
danielgomezdidier Avatar asked Jan 12 '10 19:01

danielgomezdidier


3 Answers

Extension methods and attributes will help you:

public class SkipPropertyAttribute : Attribute
{
}

public static class TypeExtensions
{
    public static PropertyInfo[] GetFilteredProperties(this Type type)
    {
        return type.GetProperties().Where(pi => pi.GetCustomAttributes(typeof(SkipPropertyAttribute), true).Length == 0).ToArray();
    }       
}

public class Test
{
    public string One { get; set; }

    [SkipProperty]
    public string Two { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var t = new Test();
        Type ty = t.GetType();

        PropertyInfo[] pinfo = ty.GetFilteredProperties();
        foreach (PropertyInfo p in pinfo)
        {
            Console.WriteLine(p.Name);
        }

        Console.ReadKey();
    }
}

UPDATE:

Little more elegant implementation of the GetFilteredProperties (thanks to Marc Gravell):

public static class TypeExtensions
{
    public static PropertyInfo[] GetFilteredProperties(this Type type)
    {
        return type.GetProperties()
              .Where(pi => !Attribute.IsDefined(pi, typeof(SkipPropertyAttribute)))
              .ToArray();
    }
}
like image 97
bniwredyc Avatar answered Oct 05 '22 18:10

bniwredyc


You could put a custom attribute on your type.

public class DoNotIncludeAttribute : Attribute
{
}

public static class ExtensionsOfPropertyInfo
{
    public static IEnumerable<T> GetAttributes<T>(this PropertyInfo propertyInfo) where T : Attribute
    {
        return propertyInfo.GetCustomAttributes(typeof(T), true).Cast<T>();
    }
    public static bool IsMarkedWith<T>(this PropertyInfo propertyInfo) where T : Attribute
    {
        return property.GetAttributes<T>().Any();
    }
}
public class Test
{
    public string One { get; set; }

    [DoNotInclude]
    public string Two { get; set; }
}

Then, in your runtime, you can search for properties that are not hidden.

foreach (var property in properties.Where(p => !p.IsMarkedWith<DoNotIncludeAttribute>())
{
    // do something...
}

It won't be really hidden, but it wouldn't show up in the enumeration.

like image 31
Jarrett Meyer Avatar answered Oct 05 '22 18:10

Jarrett Meyer


I'm not sure what the domain is here, so I'm going out on a limb...

Usually what you want to do is use Attributes to tag the properties to include in your metadata searching, not the other way around.

like image 21
Jon Seigel Avatar answered Oct 05 '22 19:10

Jon Seigel