Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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# - Fatal编程技术网

C# 使用变量作为类型

C# 使用变量作为类型,c#,C#,是否有可能使这些代码正常工作 private List<Type> Models = new List<Type>() { typeof(LineModel), typeof(LineDirectionModel), typeof(BusStopTimeModel), typeof(BusStopNameModel) }; foreach (Type model in Models) // in code of my me

是否有可能使这些代码正常工作

    private List<Type> Models = new List<Type>()
    {
        typeof(LineModel), typeof(LineDirectionModel), typeof(BusStopTimeModel), typeof(BusStopNameModel)
    };

    foreach (Type model in Models) // in code of my method
    {
        Connection.CreateTable<model>(); // error: 'model' is a variable but is used like a type
    }
private List Models=new List()
{
typeof(LineModel)、typeof(LineDirectionModel)、typeof(BusStopTimeModel)、typeof(BusStopNameModel)
};
foreach(Models中的Type model)//在我的方法的代码中
{
Connection.CreateTable();//错误:“model”是一个变量,但与类型一样使用
}

提前感谢

您将无法使用常规语法(
CreateTable
)将变量用作泛型类型。在不知道
CreateTable
做什么的情况下,您有两个选项:

  • 与其将
    CreateTable
    作为通用方法,不如将其作为参数:

    public static void CreateTable(Type modelType)
    {
    }
    
  • 使用反射使用所需类型动态调用泛型方法:

    var methodInfo = typeof (Connection).GetMethod("CreateTable");
    foreach (Type model in Models)
    {
        var genericMethod = methodInfo.MakeGenericMethod(model);
        genericMethod.Invoke(null, null); // If the method is static OR
        // genericMethod.Invoke(instanceOfConnection, null); if it's not static
    }
    

  • 请注意,反射方式会慢一些,因为方法信息在运行时才会解析。

    您可以这样做

    private List<Type> Models = new List<Type>()
    {
        typeof(LineModel), typeof(LineDirectionModel), typeof(BusStopTimeModel), typeof(BusStopNameModel)
    };
    
    void SomeMethod()
    {
      MethodInfo genericFunction =Connection.GetType().GetMethod("CreateTable");
    
      foreach (Type model in Models) 
      {
        MethodInfo realFunction = genericFunction.MakeGenericMethod(model);
        var ret = realFunction.Invoke(Connection, new object[] {  });
      }
    }
    
    private List Models=new List()
    {
    typeof(LineModel)、typeof(LineDirectionModel)、typeof(BusStopTimeModel)、typeof(BusStopNameModel)
    };
    void方法()
    {
    MethodInfo genericFunction=Connection.GetType().GetMethod(“CreateTable”);
    foreach(模型中的类型模型)
    {
    MethodInfo realFunction=genericFunction.MakeGenericMethod(模型);
    var ret=realFunction.Invoke(连接,新对象[]{});
    }
    }
    
    是的,但没有像这样简单的方法。出于性能原因,我建议您为所需的所有类型编写所有代码。我认为反射是有办法的,但它会很慢而且不容易实现。你必须使用反射的
    MakeGenericMethod
    。也许有更好的方法来做你想做的事。但是你可以。
    CreateTable
    我想应该可以。@SamedTarıkıETİN不,它不起作用。typeof(model)直到运行时才真正得到解析,但编译器需要在编译时知道确切的类型。@PoweredByOrange我明白了。感谢您提供的信息。正是因为它比较慢,所以您应该将
    typeof(Connection).GetMethod(“CreateTable”)
    移到
    foreach
    之外,并且只调用
    MakeGenericMethod
    Invoke
    内部。@xanatos很好,已修订。