方法的C#自定义属性,包括使用关键字

时间:2019-05-11 10:07:14

标签: c# attributes using

我需要准备一个可以用作Attribute语句的自定义using

所以,下面的方法

private async Task<List<TermUserEmailInformationDto>> GetAllTermUserEmailAddressesHedefBelirlemeDirektorVeUstu()
{
    using (_unitOfWorkManager.Current.DisableFilter(GlobalConstants.DataFilters.MustHaveTermId))
    {
        // Code goes on...
    }
}

应该像这样:

[DisableFilter(GlobalConstants.DataFilters.MustHaveTermId)]
private async Task<List<TermUserEmailInformationDto>> GetAllTermUserEmailAddressesHedefBelirlemeDirektorVeUstu()
{
    // Code goes on...
}

有什么办法吗?

1 个答案:

答案 0 :(得分:0)

我建议这样的属性:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
internal class DisableFilterAttribute : Attribute
{
    public DataFilter DataFilter { get; }
    public DisableFilterAttribute(DataFilter dataFilter) => DataFilter = dataFilter;
}

enum DataFilter
{
    MustHaveTermId,
    SomeFilter,
    AnotherFilter
}

注意属性参数的限制。

根据问题UnitOfWorkManager中的代码应如下所示:

internal class UnitOfWorkManager : IDisposable
{
    public UnitOfWorkManager Current { get; private set; }

    public UnitOfWorkManager()
    {
        Current = this;
    }

    public UnitOfWorkManager DisableFilter(DataFilter dataFilter)
    {
        var manager = new UnitOfWorkManager();
        Console.WriteLine($"Created  UnitOfWorkManager {manger.GetHashCode()} with {dataFilter} disabled");
        return manager;
    }

    public void Dispose() { Console.WriteLine($"Disposed UnitOfWorkManager {GetHashCode()}"); }
}

我建议使用以下方法修改UnitOfWorkManager

public void DoWithFilters(Action action)
{
    var current = Current;

    var managers = new List<UnitOfWorkManager>();
    foreach (var attribute in
                new StackTrace().GetFrame(1).GetMethod().GetCustomAttributes(false).OfType<DisableFilterAttribute>())
        managers.Add(Current = Current.DisableFilter(attribute.DataFilter));

    action();

    managers.Reverse();
    foreach (var manager in managers)
        manager.Dispose();

    Current = current;
}

一个人可以这样使用它:

[DisableFilter(DataFilter.MustHaveTermId)]
[DisableFilter(DataFilter.SomeFilter)]
private Task<List<TermUserEmailInformationDto>> GetAllTermUserEmailAddressesHedefBelirlemeDirektorVeUstu()
{
    Task<List<TermUserEmailInformationDto>> result = null;
    _unitOfWorkManager.DoWithFilters(() =>
    {
        Console.WriteLine("Performing GetAllTermUserEmailAddressesHedefBelirlemeDirektorVeUstu");
        // Code goes on...
    });
    return result;
}

运行

Console.WriteLine($"Before method called Current is {_unitOfWorkManager.Current.GetHashCode()}");
GetAllTermUserEmailAddressesHedefBelirlemeDirektorVeUstu();
Console.WriteLine($"After  method called Current is {_unitOfWorkManager.Current.GetHashCode()}");

给出如下结果:

Before method called Current is 46104728
Created  UnitOfWorkManager 21083178 with MustHaveTermId disabled
Created  UnitOfWorkManager 30015890 with SomeFilter disabled
Performing GetAllTermUserEmailAddressesHedefBelirlemeDirektorVeUstu
Disposed UnitOfWorkManager 30015890
Disposed UnitOfWorkManager 21083178
After  method called Current is 46104728

此处是指向in one piece上方代码的链接