繁体   English   中英

在C#中通过反射进行迭代时,如何跳过某些类字段?

[英]How to skip some of the class fields while iterating by reflection in C#?

我有那些与ASP.NET MVC视图紧密绑定的类。 针对它们的字段/属性(列),我必须使用Guid.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; }
}

我该如何实现?

这是如何使用它的完整示例:

[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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM