CSharp · Console
using System;
using System.Collections.Generic;
namespace ConsoleApplication2
{
abstract class Stat
{
public abstract string GetPrefix();
}
class SomeStat : Stat
{
public SomeStat()
{
}
public SomeStat(Test cont, string name)
{
//...
}
public override string GetPrefix()
{
return "SomePrefix";
}
}
class Test
{
Dictionary<string, Stat> statDict = new Dictionary<string, Stat>();
public void AddStat<T>(string statName, Func<Test, string, T> constructor) where T : Stat, new()
{
string prefix = new T().GetPrefix();
if (!statDict.ContainsKey(prefix + statName))
{
statDict.Add(prefix + statName, constructor(this, statName));
}
}
}
public class Program
{
public static void Main(string[] args)
{
Test t = new Test();
t.AddStat("Stat1", (cont, name) => new SomeStat(cont, name));
}
}
}