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