如何在c#中通过反射迭代时跳过一些类字段

本文关键字:字段 迭代 反射 | 更新日期: 2023-09-27 18:17:08

我有一些与ASP紧密绑定的类。. NET MVC视图。对于它们的字段/属性(列),我必须使用guide . newguid()生成一个唯一的名称。通过这种方式,每次视图打开时,都会有一个新的唯一名称,对应于类中与特定字段/列相关联的每个控件。

但是,我想在生成唯一名称时跳过一些属性。因为,这些属性要么是隐藏的输入,要么是用于其他特定目的的占位符。有什么好办法呢?我应该为此应用自定义属性吗?在字段迭代期间,我会直接跳过这些字段。

例如类是"

public abstract class DashboardModuleCommonSettings
{        
    public int ForwarderId { get; set; }
    public int ClientSubsidiaryId { get; set; }
    public bool IsContentUpdateable { get; set; }
    public int? AfterTime { get; set; }
    public string Title { get; set; }
    [Not Required to be iterated for generating unique name]
    public string ModuleSettingsPopupName { get; set; }
    [Not Required to be iterated for generating unique name]
    public int ClientId { get; set; }
    [Not Required to be iterated for generating unique name]
    [HiddenInput(DisplayValue = false)]
    public string CurrentLayout { get; set; }
}

我怎样才能做到这一点?

如何在c#中通过反射迭代时跳过一些类字段

下面是一个完整的示例:

[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public class NotRequiredForUniqueNameAttribute : Attribute { }

NotRequiredForUniqueNameAttribute应用于您不想使用的属性,因此您的类变成:

public abstract class DashboardModuleCommonSettings
{        
    public int ForwarderId { get; set; }
    public int ClientSubsidiaryId { get; set; }
    public bool IsContentUpdateable { get; set; }
    public int? AfterTime { get; set; }
    public string Title { get; set; }
    [NotRequiredForUniqueName]
    public string ModuleSettingsPopupName { get; set; }
    [NotRequiredForUniqueName]
    public int ClientId { get; set; }
    [NotRequiredForUniqueName]
    [HiddenInput(DisplayValue = false)]
    public string CurrentLayout { get; set; }
}

然后,当您想要提取不具有该属性的属性时,您可以这样做:

public class TestClass
{
    public static string GenerateUniqueName(DashboardModuleCommonSettings dmcs)
    {
        var propInfos = dmcs.GetType().GetProperties(
            BindingFlags.Instance | BindingFlags.Public).Where(
            p => p.GetCustomAttribute<NotRequiredForUniqueNameAttribute>() == null);
        string uniqueName = "";
        foreach (var propInfo in propInfos)
        {
            //Do something with the property info
        }
        return uniqueName;
    }
}

一种可能的方法是定义自定义属性并忽略已分配的属性。

public sealed class SkipPropertyAttribute: Attribute
{  
}

在你的课堂上:

public abstract class DashboardModuleCommonSettings
{        
    public int ForwarderId { get; set; }
    public int ClientSubsidiaryId { get; set; }
    public bool IsContentUpdateable { get; set; }
    public int? AfterTime { get; set; }
    public string Title { get; set; }
    [SkipProperty]
    public string ModuleSettingsPopupName { get; set; }
    [SkipProperty]
    public int ClientId { get; set; }
    [SkipProperty]
    [HiddenInput(DisplayValue = false)]
    public string CurrentLayout { get; set; }
}

您可以使用Attribute.IsDefined方法来查找是否定义了属性

为了子孙后代,这里是没有标志的脏版本。

public abstract class DashboardModuleCommonSettings
{
    public int ForwarderId { get; set; }
    public int ClientSubsidiaryId { get; set; }
    public bool IsContentUpdateable { get; set; }
    public int? AfterTime { get; set; }
    public string Title { get; set; }
    public string __marker__ { get; }
    public string ModuleSettingsPopupName { get; set; }
    public int ClientId { get; set; }
    public string CurrentLayout { get; set; }
}
public static class Extractor
{
    public static IEnumerable<PropertyInfo> VisibleProperties<T>()
    {
        foreach (var p in typeof(T).GetProperties())
        {
            if ("__marker__".Equals(p.Name, StringComparison.InvariantCultureIgnoreCase)) yield break;
            yield return p;
        }
    }
}