GetType()。当变量是字符串时,IsClass返回true [英] GetType().IsClass returns true when variable is string

查看:120
本文介绍了GetType()。当变量是字符串时,IsClass返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个函数来确定变量类型是类,枚举还是系统。



当我要求一种类型为string的变量时,返回IsClass = true


我正在尝试< T>说< System.Int32>同样的事情发生了,但不是当它是一个定义为Int的变量时。


为什么? 确定何时变量类型而不是System.Type的正确方法是什么?


string MyVariable =" 1" ;; bool IsClass = MyVariable.GetType()。IsClass; //返回true

test< System.Int32>(); function test< T>(){bool IsClass = typeof(T).GetType()。IsClass; } //返回true


int i = 10; bool IsClass = i.GetType()。IsClass; //返回false



解决方案

这是正确的。字符串是.Net中的一个类:


https://docs.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.8


public 密封 字符串
ICloneable IComparable IComparable < 字符串>,
IConvertible IEquatable < 字符串>,
系统
集合通用 IEnumerable < char >


I'm doing a function that determines whether a variable type is a class, an enumeration or a system.type

When I ask for a type of variable of type string returns IsClass = true

I'm trying with <T> saying <System.Int32> and the same thing happens, but not when it's a variable defined as Int.

Why? What is the correct way to determine when a type of variable is a class but not System.Type?

string MyVariable = "1"; bool IsClass = MyVariable.GetType().IsClass; // Returns true
test<System.Int32>(); function test<T>(){ bool IsClass = typeof(T).GetType().IsClass; } // Returns true
int i = 10; bool IsClass = i.GetType().IsClass; // Returns false

解决方案

This is correct. The string is a class in .Net:

https://docs.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.8

public sealed class String : ICloneable, IComparable, IComparable<string>, IConvertible, IEquatable<string>, System.Collections.Generic.IEnumerable<char>


这篇关于GetType()。当变量是字符串时,IsClass返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆