为了账号安全,请及时绑定邮箱和手机立即绑定

你好,求问关于typeof(class)的问题?

你好,求问关于typeof(class)的问题?

慕丝7291255 2021-07-14 11:11:52
Type 对象 解释下呗
查看完整描述

2 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

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

System.Type type = typeof(int);

备注

若要获取表达式的运行时类型,可以使用 .NET Framework 方法 GetType,如以下示例中所示:

int i = 0;
System.Type type = i.GetType();

不能重载 typeof
运算符。

typeof
运算符也能用于公开的泛型类型。 具有不止一个类型参数的类型的规范中必须有适当数量的逗号。
下面的示例演示如何确定方法的返回类型是否是泛型 IEnumerable<T>。 假定此方法是 MethodInfo
类型的实例:

string s = method.ReturnType.GetInterface
(typeof(System.Collections.Generic.IEnumerable<>).FullName);

示例

C#

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
*/

此示例使用 GetType
方法确定用来包含数值计算的结果的类型。 这取决于结果数字的存储要求。

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()
);
}
}



查看完整回答
反对 回复 2021-07-19
?
qq_花开花谢_0

TA贡献1835条经验 获得超6个赞

type类型为班级。

查看完整回答
反对 回复 2021-07-19
  • 2 回答
  • 0 关注
  • 386 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信