【问题标题】:how to get user input as an class type? [duplicate]如何将用户输入作为类类型? [复制]
【发布时间】:2014-11-16 12:37:37
【问题描述】:

我有如下代码。它使用读取用户输入作为表名,然后根据输入作为类类型获取类属性信息。问题是 typeof(tableName) 不正确。任何人都可以帮助我将不胜感激。

void Main()
{
    var tableName = Console.ReadLine();

    var propertyInfo = this.GetTableProperties(tableName);
}

public PropertyInfo[] GetTableProperties(string tableName)
{
    PropertyInfo[] props = typeof(tableName).GetProperties();
    return props;
}

【问题讨论】:

  • 什么是tableName,它是自定义类的全限定命名空间吗?

标签: c# reflection


【解决方案1】:

Type.GetType 可以提供基于字符串的类型,但需要完全限定名称:

Type t = Type.GetType("Fully.Qualified.Type.Name.Here");

【讨论】:

  • 如果类型在当前程序集或mscorlib之外,则需要Assembly.Qualified.Type.Name.There
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
  • 1970-01-01
  • 2014-02-13
相关资源
最近更新 更多