You can use reflection to do what you want:
var method = typeof(GameObject).GetMethod("GetComponent", new Type[] {});
var specific = method.MakeGenericMethod(typeof(Rifle));
var instance = specific.Invoke(shooter, null) as IWeapon;
eg.
using UnityEngine;
using System;
using System.Reflection;
using NUnit.Framework;
public interface IWeapon
{
void ShootAt(GameObject target);
}
public class Rifle : MonoBehaviour, IWeapon
{
public void ShootAt(GameObject target)
{
Debug.Log("Rifle");
}
}
public class Shotgun : MonoBehaviour, IWeapon
{
public void ShootAt(GameObject target)
{
Debug.Log("Shotgun");
}
}
public class WeaponTests
{
private GameObject Shooter()
{
var foo = new GameObject();
foo.AddComponent<Shotgun>();
foo.AddComponent<Rifle>();
return foo;
}
private MethodInfo method = null;
private IWeapon GetWeaponByName(GameObject shooter, string name)
{
if (method == null)
{
// This is slow, cache the result
method = typeof(GameObject).GetMethod("GetComponent", new Type[] {});
}
if (name == "Rifle")
{
MethodInfo specific = method.MakeGenericMethod(typeof(Rifle));
return specific.Invoke(shooter, null) as IWeapon;
}
else if (name == "Shotgun")
{
MethodInfo specific = method.MakeGenericMethod(typeof(Shotgun));
return specific.Invoke(shooter, null) as IWeapon;
}
return null;
}
[Test]
public void TestGetWeaponByName()
{
var target = new GameObject();
var fixture = Shooter();
IWeapon weapon;
weapon = GetWeaponByName(fixture, "Rifle");
Assert.True(weapon != null);
weapon.ShootAt(target);
weapon = GetWeaponByName(fixture, "Shotgun");
Assert.True(weapon != null);
weapon.ShootAt(target);
weapon = GetWeaponByName(fixture, "Laster");
Assert.True(weapon == null);
}
}
Notice also there's no particular need for IWeapon. You could as easily directly load the specific instances:
MethodInfo specific = method.MakeGenericMethod(typeof(Rifle));
var rifle = specific.Invoke(shooter, null) as Rifle;
rifle.RifleSpecificThing();