【问题标题】:GetType from object is returning RuntimeType [duplicate]来自对象的 GetType 正在返回 RuntimeType [重复]
【发布时间】:2012-10-21 12:48:43
【问题描述】:

我有一个简单的函数:

public string getType(object obj) {
    Type type = obj.getType();
    return type.FullName;
}

如果在运行时创建的字符串对象上使用此函数,该函数将返回“System.RuntimeType”...

但它应该返回“System.String”...

【问题讨论】:

  • 除非您向我们展示什么调用了您的 getType 方法,以及传递给它的确切内容,否则我们无法为您提供帮助。
  • typeOf 方法是传递的任何对象,可以创建。例如字符串、整数、MyClass1、MyClass2...

标签: c#


【解决方案1】:

如果你这样称呼它-

string a = "";
string type = getType(a);

它将返回System.String

但是如果你这样打电话 -

string a = "";
string type = getType(a.GetType());

然后它会返回System.RuntimeType

另外,你的方法中有一个小的typo -

Type type = obj.getType(); 应该是Type type = obj.GetType();

【讨论】:

    【解决方案2】:

    我猜你是这样称呼它的:getType(typeof(string))typeof(abc)Type 类型的值(或 RuntimeType,这是一个实现细节)。

    这样称呼它:

    getType("")

    【讨论】:

      猜你喜欢
      • 2010-10-17
      • 1970-01-01
      • 2020-09-11
      • 2019-12-15
      • 2020-05-29
      • 2020-07-05
      • 1970-01-01
      • 2018-04-01
      • 2011-06-13
      相关资源
      最近更新 更多