-2

I have an problem: I am trying:

    private struct QueuedFile
    {
        public Type t;
        public object LoadedFile;
        public string path;
        public bool loaded;
        public ContentManager c;
        public QueuedFile(string path, Type t, ContentManager c)
        {
            this.t = t;
            this.path = path;
            LoadedFile = null;
            loaded = false;
            this.c = c;
        }
        public void Load()
        {
            LoadedFile = c.Load<this.t>(path); //<--ERROR: <this.t>
            loaded = true;
        }
    }

But this gives me an error. Has anyone an idea how to save the variable T for an method like:

public T LoadFileByType<T>(string path);
user3412531
  • 27
  • 1
  • 6

1 Answers1

1

It's not that simple. Generic methods are bound at compile time. There's not a way other then reflection to bind to a generic method with a variable type.

Here's how you would do it with reflection:

MethodInfo method = c.GetType()
                     .GetMethod("Load");
                     .MakeGenericMethod(this.t);

LoadedFile = method.Invoke(path, new object[] {path});
D Stanley
  • 149,601
  • 11
  • 178
  • 240