简体   繁体   English

C# 打印 TableEntity 属性,但忽略具有属性 [IgnoreProperty] 的那些属性

[英]C# Print TableEntity Properties, but ignore those properties with attribute [IgnoreProperty]

I am trying to print out an object that implements TableEntity class, without those that should be Ignored regarding the persistence.我正在尝试打印出实现 TableEntity class 的 object,而没有关于持久性应该忽略的那些。 The approach I generally use to print out objects is to use the StatePrinter.我通常用来打印对象的方法是使用 StatePrinter。

public class MyEntity : TableEntity
{
    public string MyProperty { get; set; }

    [IgnoreProperty]
    public string MyIgnoredProperty { get; set; }

    public override string ToString()
    {
        Stateprinter printer = new Stateprinter();
        return printer.PrintObject(this);
    }
}

While this works pretty good for any kind of classes, with this MyEntity class it also prints the MyIgnoredProperty.虽然这对任何类型的类都非常有效,但使用这个 MyEntity class 它还可以打印 MyIgnoredProperty。 Is there a clever way to also ignore the properties that have [IgnoredProperty] as attribute when printing out the object?打印出 object 时,是否有一种巧妙的方法可以忽略具有 [IgnoredProperty] 作为属性的属性?

You can configure what fields/properties the Stateprinter cares about by configuring what "field harvester" to use.您可以通过配置要使用的“字段采集器”来配置Stateprinter关心的字段/属性。

Here's a simple field harvester that only returns public properties without the ' IgnoreProperty ' attribute.这是一个简单的字段采集器,它只返回没有“ IgnoreProperty ”属性的公共属性。

class PersistencePropertiesHarvester : IFieldHarvester
{
    public bool CanHandleType(Type type)
    {
        return typeof(TableEntity).IsAssignableFrom(type);
    }

    public List<SanitizedFieldInfo> GetFields(Type type)
    {
        var fields = new HarvestHelper().GetFieldsAndProperties(type);
        return fields.Where(IsPerstistenceProperty).ToList();
    }

    private static bool IsPerstistenceProperty(SanitizedFieldInfo field)
    {
        return
            // Only return properties ...
            field.FieldInfo.MemberType == MemberTypes.Property          
            &&
            // ... that has a public get method ...
            (field.FieldInfo as PropertyInfo)?.GetGetMethod(false) != null
            &&
            // ... that does not have the IgnoreProperty attribute
            field.FieldInfo.GetCustomAttribute<IgnoreProperty>() == null
            ;           
    }
}

Then you use it like this:然后你像这样使用它:

public class MyEntity : TableEntity
{
    public string MyProperty { get; set; }

    [IgnoreProperty]
    public string MyIgnoredProperty { get; set; }

    public override string ToString()
    {
        Stateprinter printer = new Stateprinter();
        printer.Configuration.Add(new PersistencePropertiesHarvester());
        return printer.PrintObject(this);
    }
}

And the result of new MyEntity().ToString() is now现在new MyEntity().ToString()的结果是

new MyEntity()
{
    MyProperty = null
}

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

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