通用扩展方法模糊 [英] Generic Extension Method Ambiguity

查看:114
本文介绍了通用扩展方法模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了两个接口:

  // IVector.cs 
公共接口IVector
{
INT尺寸{搞定; }

本浮动[INT指数] {搞定;组; }
}

// IMatrix.cs
公共接口IMatrix
{
INT尺寸{搞定; }

本浮动[INT行,诠释列] {搞定;组; }
}



以及针对这些接口的扩展方法

  // VectorExtensions.cs 
公共静态不要再增加< T>(此T载体,T值),其中T:结构,IVector
{
无功输出=默认(T);

的for(int i = 0; I< output.Size;我++)
输出[I] =向量[I] +值[I]

返回输出;
}

// MatrixExtensions.cs
公共静态不要再增加< T>(这件T矩阵,T值),其中T:结构,IMatrix
{
无功输出=默认(T);

的for(int i = 0; I< output.Size;我++)
输出[为(; J< output.Size J ++ INT J = 0)
I,J] =向量[I,J] +值[I,J]。

返回输出;
}



所有类型是相同的命名空间。



由于某些原因,当调用Add()方法从IVector衍生的东西,编译器不能确定是否使用了MatrixExtensions类中定义或VectorExtensions类。移动扩展类不同的命名空间中的一个停止错误...但我还挺希望他们在相同的命名空间:D



这究竟是为什么。

编辑:(我不能相信我忘了补充这一点)结果
我应该怎么做才能解决这个


解决方案

找到我自己的答案再次(这是一个黑客攻击的一位):

  // IVector.cs 
公共接口IVector< T>
,其中T:IVector< T>
{
INT尺寸{搞定; }

本浮动[INT指数] {搞定;组; }
}

// IMatrix.cs
公共接口IMatrix< T>
,其中T:IMatrix< T>
{
INT尺寸{搞定; }

本浮动[INT行,诠释列] {搞定;组; }
}

// VectorExtensions.cs
公众不要再增加< T>(这IVector< T>向量,T值)
,其中T:结构,IVector< ; T>
{
无功输出=默认(T);

的for(int i = 0; I< output.Size;我++)
输出[I] =向量[I] +值[I]

返回输出;
}

// MatrixExtensions.cs
公共静态不要再增加< T>(这IMatrix< T>矩阵,T值)
,其中T:结构,IMatrix< ; T>
{
无功输出=默认(T);

的for(int i = 0; I< output.Size;我++)
输出[为(; J< output.Size J ++ INT J = 0)
I,J] =向量[I,J] +值[I,J]。

返回输出;
}



它精美的作品。万岁 CRTP :D


I have two interfaces defined:

// IVector.cs
public interface IVector
{
    int Size { get; }

    float this[int index] { get; set; }
}

// IMatrix.cs
public interface IMatrix
{
    int Size { get; }

    float this[int row, int column] { get; set; }
}

As well as extension methods for those interfaces

// VectorExtensions.cs
public static T Add<T>(this T vector, T value) where T : struct, IVector
{
    var output = default(T);

    for (int i = 0; i < output.Size; i++)
        output[i] = vector[i] + value[i];

    return output;
}

// MatrixExtensions.cs
public static T Add<T>(this T matrix, T value) where T : struct, IMatrix
{
    var output = default(T);

    for (int i = 0; i < output.Size; i++)
        for (int j = 0; j < output.Size; j++)
            output[i, j] = vector[i, j] + value[i, j];

    return output;
}

All the types are in the same namespace.

For some reason, when calling Add() on something derived from IVector, the compiler can't determine whether to use the definition in the MatrixExtensions class or the VectorExtensions class. Moving one of the extension classes to a different namespace stops the errors... but I kinda want them in the same namespace :D

Why is this happening?

EDIT: (I can't believe I forgot to add this)
What should I do to work around this?

解决方案

Found my own answer again (and it's a bit of a hack):

// IVector.cs
public interface IVector<T>
    where T : IVector<T>
{
    int Size { get; }

    float this[int index] { get; set; }
}

// IMatrix.cs
public interface IMatrix<T>
    where T : IMatrix<T>
{
    int Size { get; }

    float this[int row, int column] { get; set; }
}

// VectorExtensions.cs
public T Add<T>(this IVector<T> vector, T value)
    where T : struct, IVector<T>
{
    var output = default(T);

    for (int i = 0; i < output.Size; i++)
        output[i] = vector[i] + value[i];

    return output;
}

// MatrixExtensions.cs
public static T Add<T>(this IMatrix<T> matrix, T value)
    where T : struct, IMatrix<T>
{
    var output = default(T);

    for (int i = 0; i < output.Size; i++)
        for (int j = 0; j < output.Size; j++)
            output[i, j] = vector[i, j] + value[i, j];

    return output;
}

It works beautifully. Hooray for CRTP :D

这篇关于通用扩展方法模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆