Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# <;类型>;作为变量_C#_Function_Variables_Types - Fatal编程技术网

C# <;类型>;作为变量

C# <;类型>;作为变量,c#,function,variables,types,C#,Function,Variables,Types,我有一个问题: 我正在努力: private struct QueuedFile { public Type t; public object LoadedFile; public string path; public bool loaded; public ContentManager c; public QueuedFile(string path, Type t, Conten

我有一个问题: 我正在努力:

    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;
        }
    }
private结构队列文件
{
公共类型t;
公共对象加载文件;
公共字符串路径;
公共图书馆;
公共内容经理c;
公共队列文件(字符串路径,类型t,ContentManager c)
{
t=t;
this.path=path;
LoadedFile=null;
加载=错误;
这个.c=c;
}
公共空荷载()
{

LoadedFile=c.Load(path);//没有那么简单。泛型方法是在编译时绑定的。除了反射之外,没有其他方法可以绑定到具有变量类型的泛型方法

下面是使用反射的方法:

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

LoadedFile = method.Invoke(path, new object[] {path});
MethodInfo method = c.GetType()
                     .GetMethod("Load");
                     .MakeGenericMethod(this.t);

LoadedFile = method.Invoke(path, new object[] {path});