Hi
I'm scripting the monster in my game.
Here's the interface for each monsters :
/// <summary>
/// Interface for monsters
/// </summary>
public interface IMonster
{
void OnUpdate();
void OnDraw();
}
An example for the RustMonster type. This source is loaded at runtime as a plain text (.cs) file and compiled on the fly. So I dont know it at compile time (the goal of scripting monsters...).
public class RustMonster : IMonster
{
public void OnUpdate()
{
Trace.WriteLine("OnUpdate()");
}
public void OnDraw()
{
Trace.WriteLine("OnDraw()");
}
}
I would like to create a new instance of RustMonster like this
// Somewhere in my code
IMonster rust = Script.CreateInstance<IMonster>("RustMonster");
CreateInstace definition
public class Script
{
/// <summary>
/// Creates an instance of T
/// </summary>
/// <typeparam name="T">Type of the instance</typeparam>
/// <param name="name">Name of the class</param>
/// <returns>An instance of or default(T)</returns>
public T CreateInstance<T>(string name)
{
if (!IsCompiled)
{
Trace.WriteLine("Script \"" + Name + "\" is not compiled. Can't create a new instance \"" + name + "\" of type \"" + typeof(T).Name + "\"");
return default(T);
}
// Get type
Type t = CompiledAssembly.GetType(name, false);
// Check interface
if (t.GetInterface(typeof(T).Name) == null)
{
Trace.WriteLine("Type \"" + Name + "\" found, but not an instance of \"" + typeof(T).Name + "\".");
return default(T);
}
// Create an instance
var ret = Activator.CreateInstance(t); // <--- Here is the problem
return ret;
}
}
How can I "cast" the return as T ?