C#如何使用泛型类的泛型方法参数

时间:2018-01-16 02:45:44

标签: c# class generics methods parameters

所以我有以下代码:

kernel void resizeImage(
texture2d<half, access::sample> sourceTexture [[texture(0)]],
texture2d<half, access::write> destTexture [[texture(1)]],
sampler samp [[sampler(0)]],
uint2 gridPosition [[thread_position_in_grid]])
{
float2 srcSize = float2(sourceTexture.get_width(0),
sourceTexture.get_height(0));
float2 destSize = float2(destTexture.get_width(0),
destTexture.get_height(0));
float2 sourceCoords = float2(gridPosition) / destSize;
/*+ The following attempts all produced a pixelated image
(no edges smoothed out like a fragment shader would)
half4 color = sourceTexture.sample(samp, sourceCoords);
float lod = srcSize.x / destSize.x;
float lod = 0.0;
float lod = 1.0;
float lod = 2.0;
float lod = 3.0;
float lod = 4.0;
float lod = 4.5;
float lod = 5.0;
float lod = 5.5;
*/
float lod = 6.0;
half4 color = sourceTexture.sample(samp, sourceCoords, level(lod));
destTexture.write(color, gridPosition);
}

我在尝试添加到someDictionary时遇到此错误:

  

参数2:无法从'T'转换为'Class1&lt; Class2&gt;'

任何关于如何解决这个问题的帮助,如果它能够解决的话会非常感激,谢谢。

路。

1 个答案:

答案 0 :(得分:0)

您应该声明泛型类。

public class ShouldOneClass<T,U> where T : Class1<U>, new() 
                                 where U : Class2, new()
{
    public Dictionary<int, T> someDictionary;
}

但是如果您不想使用泛型类,请将generics类型替换为interface。