《深入理解C#》——高级泛型(第三章)

1、静态字段

从属于声明它的类型,每个封闭类型都有它自己的静态字段集

//定义包含静态字段的泛型类型
public class TypeWithField<T>
{
	public static string field;

	public static void PrintField()
	{
		Console.WriteLine(field + " : " + typeof(T).Name);
	}
}

//调用
TypeWithField<int>.field = "first";
TypeWithField<string>.field = "second";
TypeWithField<DateTime>.field = "Third";          

TypeWithField<int>.PrintField();
TypeWithField<string>.PrintField();
TypeWithField<DateTime>.PrintField();
TypeWithField<int>.PrintField();

输出

从实例中可以看出,int类型对应的TypewithField类型的值一直是"first : Int32",虽然中间有设置过string类型和datetime类型,

所以每种参数类型对应的TypeWithField实例都有自己的静态字段值,多个实例的静态字段是相互隔离的

2、静态构造函数

//定义包含静态构造函数的泛型类型
public class Outer<T>
{
	public class Inner<U, V>
	{
		static Inner()
		{
			Console.WriteLine("Outer<{0}>.Inner<{1},{2}>", typeof(T).Name, typeof(U).Name, typeof(V).Name);
		}

		public static void DummyMethod() { }
	}
}

//调用
Outer<int>.Inner<string, DateTime>.DummyMethod();
Outer<string>.Inner<int, int>.DummyMethod();
Outer<object>.Inner<string, object>.DummyMethod();
Outer<string>.Inner<string, object>.DummyMethod();
Outer<object>.Inner<string, string>.DummyMethod();
Outer<string>.Inner<int,int>.DummyMethod();

输出:

 可以从输出结果可以看出,只输出了五行结果,第6行不会输出,是因为调用的第二行和第六航是一样的。和非泛型类型一样,任何封闭类型的静态构造函数只执行一次。

3、GetGenericTypeDefinition和MakeGenericType使用

string listTypeName = "System.Collections.Generic.List`1";

Type defByName = Type.GetType(listTypeName);

Type closedByName = Type.GetType(listTypeName + "[System.String]");
//MakeGenericType将泛型的类型设置为string
Type closedByMethod = defByName.MakeGenericType(typeof(string));

Type closedByTypeof = typeof(List<string>);

Console.WriteLine(closedByMethod == closedByName);
Console.WriteLine(closedByName == closedByTypeof);

Type defByTypeof = typeof(List<>);
//GetGenericTypeDefinition获取的是泛型类型的定义,不管List<T>中的T是string还是int都返回list
Type defByMethod = closedByName.GetGenericTypeDefinition();

Console.WriteLine(defByMethod == defByName);
Console.WriteLine(defByName == defByTypeof);

输出:

4、通过反射获取和调用泛型方法

静态类型

//定义包含泛型方法的静态类型
static class Ticket
{
	public static void Buy<T>()
	{
		Console.WriteLine("buy ticket success");
	}
}

//静态类型调用实例
Type type = typeof(Ticket);
MethodInfo definition =  type.GetMethod("Buy");            
MethodInfo constructed =  definition.MakeGenericMethod(typeof(string));
constructed.Invoke(null, null);

非静态类型

//定义包含泛型方法的非静态类型
class Lesson
{
	public void Learn<T>()
	{
		Console.WriteLine("Learning Lesson");
	}
}

//非静态类型调用实例
Type type2 = typeof(Lesson);
MethodInfo definition2 = type2.GetMethod("Learn");
//需要传入一个实例
object obj2 = Activator.CreateInstance(type2);
MethodInfo constructed2 = definition2.MakeGenericMethod(typeof(string));
constructed2.Invoke(obj2, null);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值