typeof 关键字
用于获取类型的 System.Type 对象。typeof 表达式采用以下形式:
System.Type type = typeof(int); |
本在线速查手册由www.w◑3◑x◑u◑e.com提供,请勿盗用!
备注
若要获取表达式的运行时类型,可以使用 .NET Framework 方法
int i = 0; System.Type type = i.GetType(); |
本在线速查手册由www.w◑3◑x◑u◑e.com提供,请勿盗用!
不能重载 typeof 运算符。
typeof 运算符也能用于公开的泛型类型。具有不止一个类型参数的类型的规范中必须有适当数量的逗号。下面的示例演示如何确定方法的返回类型是否是泛型
string s = method.ReturnType.GetInterface (typeof(System.Collections.Generic.IEnumerable<>).FullName |
本在线速查手册由www.w◑3◑x◑u◑e.com提供,请勿盗用!
示例
C# | |
---|---|
public class SampleClass2 { public int sampleMember; public void SampleMethod() {} static void Main() { Type t = typeof(SampleClass); // Alternatively, you could use // SampleClass obj = new SampleClass(); // 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: System.Type GetType() System.String ToString() Boolean Equals(System.Object) Int32 GetHashCode() Members: System.Type GetType() System.String ToString() Boolean Equals(System.Object) Int32 GetHashCode() Void .ctor() Void .ctor(Int32, System.String) System.String name Int32 id */ |
本在线速查手册由www.w◑3◑x◑u◑e.com提供,请勿盗用!
此示例使用
C# | |
---|---|
class GetTypeTest { static void Main() { int radius = 3; Console.WriteLine("Area = {0}", radius * radius * Math.PI); Console.WriteLine("The type is {0}", (radius * radius * Math.PI).GetType() ); } } /* Output: Area = 28.2743338823081 The type is System.Double */ |
本在线速查手册由www.w◑3◑x◑u◑e.com提供,请勿盗用!