c# 之 System.Type.GetType()与Object.GetType()与typeof比较
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比较的更多相关文章
- C# typeof() 和object.GetType() 、Type..GetType()使用和区别
进行学习到表达树了,用动Tpye了.所以整理了以下他们区别和用法 总得来说他们都是为了获取某个实例具体引用的数据类型System.Type.1.GetType()方法继承自Object,所以C#中任何 ...
- 反射,System.Type类
http://m.blog.csdn.net/blog/woddle/40623333 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到 ...
- 反射 介绍System.Type类
本节先介绍system.Type类,通过这个类可以访问关于任何数据类型的信息. 1. system.Type类以前把Type看作一个类,但它实际上是一个抽象的基类.只要实例化了一个Type对象,实际上 ...
- 类库探源——System.Type
一.MSDN 描述 Type 类:表示类型声明:类类型.接口类型.数组类型.值类型.枚举类型.类型参数.泛型类型定义.以及开放或封闭构造的泛型类型. 命名空间: System 程序集:mscorlib ...
- 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 ...
- 部署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 ...
- 【典型错误】The type java.lang.Object cannot be resolved.
参考:http://blog.csdn.net/wo519074786/article/details/7697967 The type java.lang.Object cannot be reso ...
- 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 ...
- The type java.lang.Object cannot be resolved
有时候在Eclipse中打开或者导入项目时会出现标题字样的问题:The type java.lang.Object cannot be resolved. It is indirectly refer ...
随机推荐
- 10.for
要遍历一个范围(如数组或表),使用 for 循环.在数组上迭代时,当前数组元素存储在循环变量中.在遍历字典时, index 存储在循环变量中. (in 内容测试) for x in [5, 7, 11 ...
- Cipher
Description Bob and Alice started to use a brand-new encoding scheme. Surprisingly it is not a Publi ...
- Hive和sparksql中的dayofweek
dayofweek在hive2.2.0开始支持 ,低版本的hive没有提供原生的dayofweek函数,有时需要用到的时候不甚方便.其实低版本的sparksql和hive中可用以下方式实现dayofw ...
- 20165215 2017-2018-2 《Java程序设计》第4周学习总结
20165215 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容总结 chapter5 子类与父类 子类的定义使用关键字extends 任何类都是Object类的 ...
- ElasticSearch相关文章推荐
1. ElasticSearch查询:http://www.cnblogs.com/ljhdo/p/4486978.html 2. Elasticsearch Java API 的使用—多条件查询:h ...
- HDU 1207 汉诺塔II (递推)
经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往上按大小顺序摞着64片黄金圆盘.上 ...
- python内置函数的简单使用和介绍
"""内置函数的简单使用和介绍参考链接:https://docs.python.org/3/library/functions.html ""&quo ...
- Django框架----跨表查询及添加记录
一:创建表 书籍模型: 书籍有书名和出版日期,一本书可能会有多个作者,一个作者也可以写多本书,所以作者和书籍的关系就是多对多的关联关系(many-to-many); 一本书只应该由一个出版商出 ...
- Q_DECL_OVERRIDE
Q_DECL_OVERRIDE也就是c++的override # define Q_DECL_OVERRIDE override 在重写虚函数时会用到, 作用是防止写错虚函数: void keyPre ...
- Scrapy小技巧-MySQL存储, MYSQL拼接
这两天上班接手,别人留下来的爬虫发现一个很好玩的 SQL脚本拼接. 只要你的Scrapy Field字段名字和 数据库字段的名字 一样.那么恭喜你你就可以拷贝这段SQL拼接脚本.进行MySQL入库处理 ...