Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法注入/交换扩展?_C#_Asp.net_Dependency Injection_Extension Methods_Mixins - Fatal编程技术网

C# 有没有办法注入/交换扩展?

C# 有没有办法注入/交换扩展?,c#,asp.net,dependency-injection,extension-methods,mixins,C#,Asp.net,Dependency Injection,Extension Methods,Mixins,我有一个asp.net web表单用于管理网格视图行格式 基本上,它们是我的代码隐藏类的一种“服务”: protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { var row = e.Row; if (row.RowType == DataControlRowType.DataRow) { decimal am

我有一个asp.net web表单用于管理网格视图行格式

基本上,它们是我的代码隐藏类的一种“服务”:

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        var row = e.Row;
        if (row.RowType == DataControlRowType.DataRow)
        {
            decimal amount = Decimal.Parse(row.GetCellText("Spend"));
            string currency = row.GetCellText("Currency");
            row.SetCellText("Spend", amount.ToCurrency(currency));
            row.SetCellText("Rate", amount.ToCurrency(currency));
            row.ChangeCellText("Leads", c => c.ToNumber());
        }
    }
与类的实例不同,它们没有用于DI容器的接口


有没有办法获得可交换扩展的功能?

不是在执行时,没有-毕竟,它们只是作为静态方法调用绑定的

如果你想把它们交换出去,你可能想考虑把它们放在接口中……< /P>


如果您愿意在编译时交换它们,只需更改您的using指令。

不是在执行时,不是-毕竟,它们只是作为静态方法调用绑定的

如果你想把它们交换出去,你可能想考虑把它们放在接口中……< /P>


如果您愿意在编译时交换它们,只需更改您的using指令。

静态类是一个横切关注点。若将静态类的实现提取到非静态类中,则可以使用静态类进行DI。然后,您可以为静态类字段指定具体的实现

嗯,我的C#更好,那么我的英语

//abstraction
interface IStringExtensions
{
    bool IsNullOrWhiteSpace(string input);
    bool IsNullOrEmpty(string input);
}

//implementation
class StringExtensionsImplementation : IStringExtensions
{
    public bool IsNullOrWhiteSpace(string input)
    {
        return String.IsNullOrWhiteSpace(input);
    }

    public bool IsNullOrEmpty(string input)
    {
        return String.IsNullOrEmpty(input);
    }
}

//extension class
static class StringExtensions
{
    //default implementation
    private static IStringExtensions _implementation = new StringExtensionsImplementation();

    //implementation injectable!
    public static void SetImplementation(IStringExtensions implementation)
    {
        if (implementation == null) throw new ArgumentNullException("implementation");

        _implementation = implementation;
    }

    //extension methods
    public static bool IsNullOrWhiteSpace(this string input)
    {
        return _implementation.IsNullOrWhiteSpace(input);
    }

    public static bool IsNullOrEmpty(this string input)
    {
        return _implementation.IsNullOrEmpty(input);
    }
}

静态类是一个交叉关注点。若将静态类的实现提取到非静态类中,则可以使用静态类进行DI。然后,您可以为静态类字段指定具体的实现

嗯,我的C#更好,那么我的英语

//abstraction
interface IStringExtensions
{
    bool IsNullOrWhiteSpace(string input);
    bool IsNullOrEmpty(string input);
}

//implementation
class StringExtensionsImplementation : IStringExtensions
{
    public bool IsNullOrWhiteSpace(string input)
    {
        return String.IsNullOrWhiteSpace(input);
    }

    public bool IsNullOrEmpty(string input)
    {
        return String.IsNullOrEmpty(input);
    }
}

//extension class
static class StringExtensions
{
    //default implementation
    private static IStringExtensions _implementation = new StringExtensionsImplementation();

    //implementation injectable!
    public static void SetImplementation(IStringExtensions implementation)
    {
        if (implementation == null) throw new ArgumentNullException("implementation");

        _implementation = implementation;
    }

    //extension methods
    public static bool IsNullOrWhiteSpace(this string input)
    {
        return _implementation.IsNullOrWhiteSpace(input);
    }

    public static bool IsNullOrEmpty(this string input)
    {
        return _implementation.IsNullOrEmpty(input);
    }
}

+这个孩子让我想起了所谓的+1这个孩子让我想起了所谓的