开发者

type function<type>()

I saw something like this in some graphics libs today, and it seems like it could be very usefull.

In the demo I saw it looked like this:

Texture2D texture = Content.Load<Texture2D>("Textures//Road");
Effect shader = Content.Load<Effect>("Effects//Road");

I assume it's a function that returns whatever type is defined in the <> brackets, and does a different action for different types.

开发者_StackOverflow中文版I want to implement something similar myself, how is it used?


It's called generics.

Example:

public T ConvertValue<T>(Object value)
{
    return (T)Convert.ChangeType(value, typeof(T));
}

References:

Generics (C# Programming Guide)


The Load method can be used to load lots of different types of content, including things like images and sounds. Therefore, it may return instances of different Types (such as your Texture2d and Effect).

There are two ways of creating a method that can do this. First, and most simply, you can return an Object, which is the base class of every type in the .NET and XNA world.

public object Load(string path) { /*...*/}

you would have to use it thusly:

Texture2D texture = (Texture2D)Content.Load("Textures//Road");

The second way is to use generics. With generics, you can handle the cast for the user, making their code simpler. A generic version of this method would be:

public T Load<T>(string path)
{
    return (T)Load(path);
}

You're still using the old load, but you're performing the cast (to whatever type that T is) prior to returning the value.

Generics are useful, as you can use them to create type safe code without creating lots and lots of type-specific method versions, like

public Texture2d LoadTexture2d(string path)
{
    return (Texture2d)Load(path);
}    
public Effect LoadEffect(string path)
{
    return (Effect)Load(path);
}
//500 more versions omitted for brevity


Its called Generics a great way to specify methods and classes for Type Independent programming. Generics allow you to define type-safe data structures, without committing to actual data types. This results in a significant performance boost and higher quality code, because you get to reuse data processing algorithms without duplicating type-specific code.

A simple example of stack:

public class Stack<T>
{
   T[] m_Items; 
   public void Push(T item)
   {...}
   public T Pop()
   {...}
}
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
int number = stack.Pop();

Above class will work for any type like string or your custom class's object Look Here for Complete Description

http://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx#csharp_generics_topica

and

http://msdn.microsoft.com/en-us/library/512aeb7t%28VS.80%29.aspx


The Load method defines indeed what it should return and so that is already defined for you. Inside the Load method, the contentLoader decides how to load it. There are a couple of XNA examples on loading your own kind of data as well. If you create something like an: .world-file. You can create your own AssetLoader that specifies how to load your own kind of data.

  1. http://create.msdn.com/en-US/education/catalog/sample/custom_model_importer
  2. http://msdn.microsoft.com/library/bb447743.aspx
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜