簡體   English   中英

使用Ninject上下文綁定時如何檢索屬性和屬性?

[英]How can I retrieve the attribute and the properties when using Ninject contextual binding?

我有一個構造函數

[ReadFromFile(@"C:\SampleData\login.json")]
public AccountController(IReadRepository<LoginMockDataModel> repository, string filePath) : base(repository)
{
}

該屬性包含屬性“ FilePath”。

public string FilePath {get;set;}

我想在上述情況下檢索“ FilePath”的值,該值為“ C:\\ SampleData \\ login.json”。

是否可以使用Ninject的IContext檢索值?

這個想法是檢索屬性的值,然后按如下所示在綁定中使用它:

// FileReadRepo contains a constructor with the argument "filePath"
// which will need a string passed to it which I am trying to retrieve
// from the Attribute above
Bind(typeof(IReadRepository<>)).To(typeof(FileReadRepo<>))
.WhenMemberHas<ReadFromFileAttribute>()
.WithConstructorArgument("filePath", CheckAttributePath);

其中CheckAttributePath是委托:

private object CheckAttributePath(IContext arg)
{
    throw new NotImplementedException();
}

我不確定如何獲取屬性的值。

通過IContext.Request.Target.Member完成訪問AccountController的構造函數。 所以這工作:

private static object CheckAttributePath(IContext context)
{
    var attributes = context.Request.Target.Member
        .GetCustomAttributes(typeof(ReadFromFileAttribute), false);
    return ((ReadFromFileAttribute)attributes[0]).Path;
}

完整的測試代碼(使用xunit和FluentAssertions):

using System;
using Ninject;
using Ninject.Activation;
using Xunit;
using FluentAssertions;

public interface IReadRepository<T>
{
    string FilePath { get; }
}

public class FileReadRepo<T> : IReadRepository<T>
{
    private readonly string filePath;

    public FileReadRepo(string filePath)
    {
        this.filePath = filePath;
    }

    public string FilePath { get { return this.filePath; } }
}

[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)]
public class ReadFromFileAttribute : Attribute
{
    public readonly string Path;

    public ReadFromFileAttribute(string path)
    {
        this.Path = path;
    }
}

public class AccountController
{
    public readonly IReadRepository<string> Repository;

    [ReadFromFile(IntegrationTest.SampleFilePath)]
    public AccountController(IReadRepository<string> repository)
    {
        this.Repository = repository;
    }
}

public class IntegrationTest
{
    public const string SampleFilePath = @"C:\SampleData\login.json";

    [Fact]
    public void Test()
    {
        var kernel = new StandardKernel();

        kernel.Bind(typeof(IReadRepository<>)).To(typeof(FileReadRepo<>))
            .WhenMemberHas<ReadFromFileAttribute>()
            .WithConstructorArgument("filePath", CheckAttributePath);

        kernel.Get<AccountController>().Repository.FilePath.Should().Be(SampleFilePath);
    }

    private static object CheckAttributePath(IContext context)
    {
        var attributes = context.Request.Target.Member.GetCustomAttributes(
            typeof(ReadFromFileAttribute), false);
        return ((ReadFromFileAttribute)attributes[0]).Path;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM