type function <type>()

Han*_*esh 3 c# xna

我今天在一些图形库中看到了类似的东西,看起来它可能非常有用.

在演示中,我看到它看起来像这样:

Texture2D texture = Content.Load<Texture2D>("Textures//Road");
Effect shader = Content.Load<Effect>("Effects//Road");
Run Code Online (Sandbox Code Playgroud)

我假设它是一个函数,它返回<>括号中定义的任何类型,并为不同类型执行不同的操作.

我想自己实现类似的东西,它是如何使用的?

dec*_*one 9

它被称为泛型.

例:

public T ConvertValue<T>(Object value)
{
    return (T)Convert.ChangeType(value, typeof(T));
}
Run Code Online (Sandbox Code Playgroud)

参考文献:

泛型(C#编程指南)