c# 之 System.Type.GetType()与Object.GetType()与typeof比较

Object.GetType()与typeof的区别

//运算符,获得某一类型的 System.Type 对象。
Type t = typeof(int);

//方法,获取当前实例的类型。
 int i = 10;
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);
                }
            }
时间: 2024-08-24 21:27:57

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

反射,System.Type类

http://m.blog.csdn.net/blog/woddle/40623333 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到的呢?B超是B型超声波,它可以透过肚皮通过向你体内发射B型超声波,当超声波遇到内脏壁的时候就会产生一定的“回音”反射,然后把“回音”进行处理就可以显示出内脏的情况了(我不是医生也不是声学专家,不知说得是否准确^_^).2.地球内部结构:地球的内部结构大体可以分为三层:地壳.地幔和地核.地壳是固体,地

System.Type.cs

ylbtech-System.Type.cs 1.返回顶部 1. #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\mscorlib.dll #endregion using System.Diag

System.Type类

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {    

System.Type反射(1/3)

通过System.Type,我们可以获取某个类的全名.命名空间.属性等信息. 实验代码: static void Main(string[] args) { //System.Type string s = "hello refelction"; Type t = s.GetType(); //方法一 Console.WriteLine(t.FullName); Type t2 = Type.GetType("system.String", false, true)

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

在用eclipse开发中,有时候需要导入别人的项目进行学习研究偶尔会出现代码无端端显示一大推错误,甚至连包名都出错的情况,这是后把鼠标移动到包名,系统就会弹出The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files的提示 原因是jdk出错. 解决方法是:更换一个jdk,操作步骤:project->properties->Java Bulid Path

杂(三)-The type java.lang.Object cannot be resolved It is indirectly referenced ...

The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files 当你在Eclipse引用不同版本JDK工程时会发生该问题.由于你开发环境中应用了多个版本的JDK 或JRE导致的.Eclipse会按照最初的开发环境默认选择对应的Jre.如Eclipse上有jdk1.4开发的环境工程,当你在引入高版本jdk1.6开发的工程时,以上问题就出现了. 问题解决的方案如下

eclipse中java文件报错: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 大概意思就是.class文件引用错误, 联想一下 编译出了问题, JRE问题. 解决办法:先在Java Bulid Path中把所有的JRE先remove掉,然后重新引入JRE即可.( 右键工程 > Build Path > Configure Build Path > )

Failed to create a 'System.Type' from the text ' ' in wpf(无法从文本创建类型)

问题描述:WPF is unable to create a type for data templateWPF使用mvvm模式无法加载DataTemplate模板定义的资源,提示无法从文本创建类型错误.原因:Resource Dictionary的编译动作被设置成了Resource,设置成Page则不会出现这个问题.解决方案:将文件的编译行为修改为“Page”. Failed to create a 'System.Type' from the text ' ' in wpf(无法从文本创建类

Jpa自定义查询报错(Failed to convert from type [java.lang.Object[]] to type)

Jpa自定义查询报错 问题背景 今天遇到一个奇怪的报错"Failed to convert from type [java.lang.Object[]] to type",这个报错,百度上也是很少的,恰恰是这样的问题,引起我了解决的欲望.先看看报错: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [o