文章内容

2017/5/8 17:42:56,作 者: 黄兵

在C#中如何判断一个变量是不是int型变量

public Type GetType()

当前实例的准确运行时类型。

用于获取类型的 System.Type 对象。 typeof 表达式采用以下形式:

System.Type type = typeof(int);


public class ExampleClass
{
public int sampleMember;
public void SampleMethod() {}

static void Main()
{
Type t = typeof(ExampleClass);
// Alternatively, you could use
// ExampleClass obj = new ExampleClass();
// Type t = obj.GetType();

Console.WriteLine("Methods:");
System.Reflection.MethodInfo[] methodInfo = t.GetMethods();

foreach (System.Reflection.MethodInfo mInfo in methodInfo)
Console.WriteLine(mInfo.ToString());

Console.WriteLine("Members:");
System.Reflection.MemberInfo[] memberInfo = t.GetMembers();

foreach (System.Reflection.MemberInfo mInfo in memberInfo)
Console.WriteLine(mInfo.ToString());
}
}
/*
Output:
Methods:
Void SampleMethod()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
Members:
Void SampleMethod()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
Void .ctor()
Int32 sampleMember
*/

下面的代码在C#中判断一个变量是不是int型变量

int a = 0;
if (a.GetType() == typeof(int))
{
MessageBox.Show("是int型");
}
C# <= 运算符
ASP.NET MVC 随想录
分享到:

发表评论

评论列表