Object.GetType()与typeof的区别

//运算符,获得某一类型的 System.Type 对象。
Type t = typeof(int); //方法,获取当前实例的类型。
int i = ;
Console.WriteLine(i.GetType());
//区别
Typeof()是运算符而GetType是方法
GetType()是基类System.Object的方法,因此只有建立一个实例之后才能被调用(也就是创建实例)
Typeof()的参数只能是lint,string,类,且不能是实例
得到结果的区别
(1)Typeof():得到一个class的Type
(2)GetType():得到一个class实例的Type

System.Type.GetType()的使用

Type type = System.Type.GetType("ConsoleApplication1.child");
Type type1 = System.Type.GetType("System.Int32");

Object.GetType()的小案例

public class Student
{
public Student()
{ }
public virtual string Id { get; set; }
public virtual string StudentNo { get; set; }
public virtual string Address { get; set; }
} public class StudentDTO
{
public StudentDTO()
{ }
public virtual string Id { get; set; }
public virtual string StudentNo { get; set; }
public virtual int TeacherId { get; set; }
}
//对student对象赋值
Student student = new Student();
student.Id = Guid.NewGuid().ToString();
student.Name = "张三";
student.Address = "福建";
//将student的值赋予studentdto
StudentDTO studentDTO = new StudentDTO();
studentDTO.Id = student.Id;
studentDTO.Name = student.Name; 改进:若是student的属性过多,那么可以通过此方法减少许多代码
foreach (var item in student.GetType().GetProperties()) //返回Student的所有公共属性
{
var value = item.GetValue(student, null); //返回属性值
var setobj = studentDTO.GetType().GetProperty(item.Name); //搜索具有指定属性名称的公共属性
if (value != null && setobj != null)
{
setobj.SetValue(studentDTO, value, null);
}
}

c# 之 System.Type.GetType()与Object.GetType()与typeof比较的更多相关文章

  1. C# typeof() 和object.GetType() 、Type..GetType()使用和区别

    进行学习到表达树了,用动Tpye了.所以整理了以下他们区别和用法 总得来说他们都是为了获取某个实例具体引用的数据类型System.Type.1.GetType()方法继承自Object,所以C#中任何 ...

  2. 反射,System.Type类

    http://m.blog.csdn.net/blog/woddle/40623333 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到 ...

  3. 反射 介绍System.Type类

    本节先介绍system.Type类,通过这个类可以访问关于任何数据类型的信息. 1. system.Type类以前把Type看作一个类,但它实际上是一个抽象的基类.只要实例化了一个Type对象,实际上 ...

  4. 类库探源——System.Type

    一.MSDN 描述 Type 类:表示类型声明:类类型.接口类型.数组类型.值类型.枚举类型.类型参数.泛型类型定义.以及开放或封闭构造的泛型类型. 命名空间: System 程序集:mscorlib ...

  5. How do I check if a type is a subtype OR the type of an object?

    To check if a type is a subclass of another type in C#, it's easy: typeof (SubClass).IsSubclassOf(ty ...

  6. 部署hibernate框架项目时出现问题:The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files.

    基本情况: (这些其实关系不大)我是直接impor导入HibernateDemo项目到eclipse中的,该项目的hibernate版本是3.6.7.Final版,使用了Hibernate Tools ...

  7. 【典型错误】The type java.lang.Object cannot be resolved.

    参考:http://blog.csdn.net/wo519074786/article/details/7697967 The type java.lang.Object cannot be reso ...

  8. The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files

    The type java.lang.Object cannot be resolved.It is indirectly referenced from required .class files ...

  9. The type java.lang.Object cannot be resolved

    有时候在Eclipse中打开或者导入项目时会出现标题字样的问题:The type java.lang.Object cannot be resolved. It is indirectly refer ...

随机推荐

  1. python ---多线程thread

    thread 在数据预处理的时候用处不大,因为有GIL 锁 查看thread信息 import threading print(threading.current_thread()) print(th ...

  2. Sitecore安装(手动方式)

    Sitecore安装 Sitecore提供手动安装压缩包(.zip)和自动安装程序包(.exe),当您运行自动安装程序时,引导界面会指导您一步步进行安装.为了让您更细致的了解Sitecore的安装配置 ...

  3. Sql server 存储过程批量插入若干数据。

    测试时,经常需要生成大量数据来测试系统性能,此功能可以用存储过程快速生成. 1. 随机生成日期 DECLARE @Date_start datetime DECLARE @Date_end datet ...

  4. linux 安装 Python

    一. 打开终端,输入:wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0b4.tgz 下载完毕后 输入解压命令:tar –zxvf Py ...

  5. scu 4444 Travel

    题意: 一个完全图,有n个点,其中m条边是权值为a的无向边,其它是权值为b的无向边,问从1到n的最短路. 思路: 首先判断1和n被哪种边连通. 如果是被a连通,那么就需要全部走b的边到达n,选择最小的 ...

  6. QString 与 string转换

    [1]QString 转换为string QString qString("好好学习天天向上"); std::string stdString = qString.toStdStr ...

  7. qq网吧弹框如何去掉?如何删掉NetBar文件夹?

    qq网吧弹框如何去掉?如何删掉NetBar文件夹?有些qq会弹出qq网吧,让人烦恼.而且点了那个不是网吧的反馈了多次都还会弹出.如何退出关闭删除取消去掉qq网吧呢,下面介绍一种解决方法:1.打开qq安 ...

  8. window下nodejs用nodemon启动koa2项目(用cmd启动不了,要用Git Bash Here 启动才可以)

    window下nodejs用nodemon启动koa2项目(用cmd启动不了,要用Git Bash Here 启动才可以)nodemon --watch 'app/**/*' -e ts --exec ...

  9. rgferg

    dfgsdfg fdvgdsafg fgdfgdfg

  10. python--表达式(运算表达式)

    运算表达式 python 的表达式包括:算术运算符,赋值运算符,比较运算符,成员运算符 算术运算符 运算符 描述 + 加 - 两个对象相加 - 减 - 得到负数或是一个数减去另一个数 * 乘 - 两个 ...