C#中通过Type类可以访问任意数据类型信息。
system.Type类以前把Type看作一个类,但它实际上是一个抽象的基类。
只要实例化了一个Type对象,实际上就实例化了Typc的一个派生类。
尽管一般情况下派生类只提供各种Type方法和属性的不同重载,但是这些方法和属性返回对应数据类型的正确数据,Type有与每种数据类型对应的派生类。
Type是许多反射功能的入口 。注意,可用的属性都是只读的:可以使用Type确定数据的类型,但不能使用它修改该类型
1.获取Type,有3种方式:
a.使用typeof运算符,如Type t = typeof(int);
b.使用GetType()方法,如int i;Type t = i.GetType();
c.使用Type类的静态方法GetType(),如Type t =Type.GetType("System.Double");
2.Type的属性:
Name:数据类型名;
FullName:数据类型的完全限定名,包括命名空间;
Namespace:数据类型的命名空间;
BaseType:直接基本类型;
UnderlyingSystemType:映射类型;
3.Type的方法:
GetMethod():返回一个方法的信息;
GetMethods():返回所有方法的信息。
GetMember()和GetMembers()方法返回数据类型的任何成员或所有成员的详细信息,不管这些成员是构造函数、属性和方法等。
4.Type的Property【属性】
GetProperty()
GetProperties():所有属性
GetValue(object, null);
SetValue(object, object1, null);
1、typeof(x)中的x,必须是具体的类名、类型名称等,不可以是变量名称。
2、GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof()相同,返回Type类型的当前对象的类型。
比如有这样一个变量i:
Int32 i = new Int32();
i.GetType()返回值是Int32的类型,但是无法使用typeof(i),因为i是一个变量,如果要使用typeof(),则只能:typeof(Int32),返回的同样是Int32的类型。
typeof是返回括号内的数据类型。
using System.Reflection;
namespace TypeStudy
{
class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return Id + "-" + Name;
}
}
class ClassCopy
{
public static void CopyData(object sourceObj, object targetObj)
{
PropertyInfo[] propertyInfos = sourceObj.GetType().GetProperties();
PropertyInfo[] propertyInfosTarget = targetObj.GetType().GetProperties();
foreach (var propertyInfo in propertyInfosTarget)
{
foreach (var propertyInfoSource in propertyInfos)
{
if (propertyInfoSource.Name == propertyInfo.Name && propertyInfoSource.PropertyType == propertyInfo.PropertyType)
{
var obj = propertyInfoSource.GetValue(sourceObj, null);
propertyInfo.SetValue(targetObj, obj, null);
}
}
}
}
}
}
using System;
using System.Reflection;
namespace TypeStudy
{
class TestMember
{
public string Name { get; set; }
public int Age { get; set; }
public void SetData()
{
this.Name = "张三";
this.Age = 30;
}
public TestMember GetData()
{
TestMember testMember = new TestMember();
testMember.SetData();
return testMember;
}
}
class Program
{
static void Main(string[] args)
{
TestMember testMember = new TestMember();
Type type = typeof(TestMember);
//Console.WriteLine(type);//直接输出type值等同于type.FullName
//Type type1 = typeof(int);
//错误Type type2 = typeof(testMember);//typeof括号内只能是类型
//错误Type type3 = typeof(i);
TestMember testMember1 = testMember.GetData();
Console.WriteLine(type.Name);
Console.WriteLine(type.FullName);
//方法
MethodInfo[] methodInfos = type.GetMethods();
foreach (var item in methodInfos)
{
Console.WriteLine(item.ToString());
Console.WriteLine(item.Name);
}
//属性
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var item in propertyInfos)
{
Console.WriteLine(item.Name);
Console.WriteLine(item.PropertyType);
Console.WriteLine("value=" + item.GetValue(testMember1, null));
//item.SetValue(testMember1, "12", null);
}
Console.WriteLine("--------");
//对象复制
User user = new User() {
Name = "离散",
Id = 1,
Age = 23
};
Person person = new Person() {
Id = 2
};
ClassCopy.CopyData(user, person);
Console.WriteLine(person);
}
}
}
自定义的三种特性:
using System;
namespace CustomAttributes.Repository.Custom
{
/// <summary>
/// 字段映射
/// @Name 当数据库名与字段名不一致时
/// </summary>
public class ColumnAttribute : Attribute
{
/// <summary>
/// 字段名不同时的映射
/// </summary>
public string Name { get; set; }
public ColumnAttribute(string name)
{
Name = name;
}
}
}
using System;
namespace CustomAttributes.Repository.Custom
{
/// <summary>
/// 仅用作查询字段,不做增删改操作
/// </summary>
public class OnlyQueryAttribute : Attribute
{ }
}
using System;
namespace CustomAttributes.Repository.Custom
{
/// <summary>
/// 主键特性
/// @IsAutoIncrement 是否自增长主键
/// </summary>
public class PrimaryKeyAttribute : Attribute
{
/// <summary>
/// 是否自增主键
/// </summary>
public bool IsAutoIncrement { get; set; }
public PrimaryKeyAttribute(bool isAutoIncrement)
{
IsAutoIncrement = isAutoIncrement;
}
}
}
使用:
using CustomAttributes.Repository.Custom;
using Dapper.Contrib.Extensions;
using System;
namespace CustomAttributes.Model
{
[Table("employee")]
public class EmployeeInfo
{
/// <summary>
/// 主键
/// </summary>
[PrimaryKey(false)]
public int EmpId { get; set; }
[Column("empNo")]
public string EmpNo { get; set; }
[OnlyQuery]
public DateTime CreateTime { get; set; }
}
}
解析使用:
using CustomAttributes.Model;
using CustomAttributes.Repository.Custom;
using Dapper.Contrib.Extensions;
using System;
using System.Reflection;
namespace CustomAttributesApp
{
class Program
{
static void Main(string[] args)
{
EmployeeInfo employeeInfo = new EmployeeInfo();
//1.获取类的特性
Type type = typeof(EmployeeInfo);
//1.1判断是否存在特性
if (type.IsDefined(typeof(TableAttribute)))
{
//1.2获取特性的值
TableAttribute attr = type.GetCustomAttribute<TableAttribute>();
Console.WriteLine(attr.Name);
}
//2.获取类属性的特性
PropertyInfo[] properties = type.GetProperties();
foreach (var item in properties)
{
//2.1自定义主键
PrimaryKeyAttribute key = item.GetCustomAttribute<PrimaryKeyAttribute>();
if (key != null)
{
Console.WriteLine(item.Name);
Console.WriteLine(key.IsAutoIncrement);
}
//2.2类属性与数据库字段不同时
ColumnAttribute column = item.GetCustomAttribute<ColumnAttribute>();
if (column != null)
{
Console.WriteLine(item.Name);
Console.WriteLine(column.Name);
}
//2.3仅用作查询的字段
OnlyQueryAttribute query = item.GetCustomAttribute<OnlyQueryAttribute>();
if (query != null)
{
Console.WriteLine(item.Name);
}
}
}
}
}
