I had a look at the code in ILSpy, and it's heavily generic. There isn't a private Read(Type type)
method available.
Your option on the PC to keep things quick is to use Expression
objects - basically compile an expression to make sure things stay quick; otherwise use reflection (which can be slower) to invoke the method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using System.Linq.Expressions;
using System.Reflection;
namespace WindowsGame1
{
static class ContentLoader
{
#if !XBOX
private static readonly Dictionary<string, Func<ContentManager, string, object>> _calls = new Dictionary<string, Func<ContentManager, string, object>>(StringComparer.Ordinal);
#endif
private static readonly MethodInfo _loadMethod = typeof(ContentManager).GetMethod("Load", BindingFlags.Public | BindingFlags.Instance);
public static object Load(this ContentManager manager, string typeName, string assetName)
{
#if XBOX
var mi = _loadMethod.MakeGenericMethod(Type.GetType(typeName, true));
return mi.Invoke(manager, new object[] { assetName });
#else
Func<ContentManager, string, object> load;
lock (_calls)
{
if (!_calls.TryGetValue(typeName, out load))
_calls.Add(typeName, load = CreateLoader(typeName));
}
return load(manager, assetName);
#endif
}
private static Func<ContentManager, string, object> CreateLoader(string typeName)
{
// Create the method info.
var type = Type.GetType(typeName, true);
var mi = _loadMethod.MakeGenericMethod(type);
var paramContentManager = Expression.Parameter(typeof(ContentManager), "contentManager");
var paramAssetName = Expression.Parameter(typeof(string), "assetName");
// Create the call and cast-down.
var call = Expression.Call(paramContentManager, mi, paramAssetName);
var convert = Expression.Convert(call, typeof(object));
var lambda = Expression.Lambda<Func<ContentManager, string, object>>(convert, paramContentManager, paramAssetName);
return lambda.Compile();
}
}
}
Remember that most-likely you will need to use the assembly-qualified type name. So for instance:
var content = Content.Load("Microsoft.Xna.Framework.Graphics.SpriteFont, Microsoft.Xna.Framework.Graphics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553", "SpriteFont1");
To get the AQFQN of a type simple inspect it:
var tn = typeof(SpriteFont).AssemblyQualifiedName;