首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从getType().GetProperties()中排除属性

从getType().GetProperties()中排除属性
EN

Stack Overflow用户
提问于 2010-01-13 03:44:10
回答 6查看 22.5K关注 0票数 28

大家好,我在一个使用C#的类库中工作,我有一些带有一些属性的类。

我只想知道我是否可以添加一些东西来从getType().GetProperties()中排除一些属性。

下面是我想要的示例:

代码语言:javascript
复制
class Test
{
    public string one { get; set; }
    public string two {get ; set;}
}

如果我这样做:

代码语言:javascript
复制
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);
       }
  }

我希望输出是这样的:

代码语言:javascript
复制
one

或者仅仅是其中一个属性。

有可能做这样的事情吗?我不知道C#中是否有某种修饰符或注释,它们允许我做我想做的事情。

谢谢。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2010-01-13 04:18:16

扩展方法和属性将帮助您:

代码语言:javascript
复制
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();
    }
}

更新:

更优雅的GetFilteredProperties实现(多亏了Marc Gravell):

代码语言:javascript
复制
public static class TypeExtensions
{
    public static PropertyInfo[] GetFilteredProperties(this Type type)
    {
        return type.GetProperties()
              .Where(pi => !Attribute.IsDefined(pi, typeof(SkipPropertyAttribute)))
              .ToArray();
    }
}
票数 37
EN

Stack Overflow用户

发布于 2010-01-13 03:54:17

您可以在您的类型上添加一个自定义属性。

代码语言:javascript
复制
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; }
}

然后,在运行时中,您可以搜索未隐藏的属性。

代码语言:javascript
复制
foreach (var property in properties.Where(p => !p.IsMarkedWith<DoNotIncludeAttribute>())
{
    // do something...
}

它不会被真正隐藏,但它不会显示在枚举中。

票数 7
EN

Stack Overflow用户

发布于 2010-01-13 03:54:18

我不确定这里的域是什么,所以我要冒险一试...

通常,您要做的是使用Attribute来标记要包括在元数据搜索中的属性,而不是反过来。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2051834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档