An Introduction to C# Generics[10]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 13121982 的 blog

public event genericeventhandler<mypublisher,eventargs> myevent;
public void fireevent()
{
myevent(this,eventargs.empty);
}
}
public class mysubscriber<argstype> //optional: can be a specific type
{
public void somemethod(mypublisher sender,argstype args)
{...}
}
mypublisher publisher = new mypublisher();
mysubscriber<eventargs> subs = new mysubscriber<eventargs>();
publisher.myevent += subs.somemethod;
code block 8 uses a generic delegate called genericeventhandler, which accepts a generic sender
type and a generic type parameter. obviously, if you need more parameters, you can simply add more
generic type parameters, but i wanted to model genericeventhandler after the non-generic .net
eventhandler, defined as:
public void delegate eventhandler(object sender,eventargs args);
unlike eventhandler, genericeventhandler is type safe, as shown in code block 8 because it
accepts only objects of the type mypublisher as senders, rather than mere object.
generics and reflection
in .net 2.0, reflection is extended to support generic type parameters. the type type can now represent
generic types with specific type parameters (called bounded types), or unspecified (unbounded) types.
as in c# 1.1, you can obtain the type of any type by using the typeof operator or by calling the
gettype() method that every type supports. regardless of the way you choose, both yield the same
type. for example, in the following code sample, type1 is identical to type2.
linkedlist<int,string> list = new linkedlist<int,string>();
type type1 = typeof(linkedlist<int,string>);
type type2 = list.gettype();
debug.assert(type1 == type2);
both typeof and gettype() can operate on naked generic type parameters:
public class myclass<t>
{
public void somemethod(t t)
{
type type = typeof(t);
debug.assert(type == t.gettype());
}
}
type has new methods and properties designed to provide reflection information about the generic
aspect of the type. code block 9 shows the new methods.
code block 9. type's generic reflection members
public abstract class type : //base types
{
public int genericparameterposition{virtual get;}
public bool hasgenericparameters{get;}
public bool hasunboundgenericparameters{virtual get;}
public bool isgenericparameter{virtual get;}
public bool isgenerictypedefinition{virtual get;}
public virtual type bindgenericparameters(type[] typeargs);
public virtual type[] getgenericparameters();
public virtual type getgenerictypedefinition();
//rest of the members
}
the most useful of these new members are the hasgenericparameters and
hasunboundgenericparameters properties, and getgenericparameters() and
getgenerictypedefinition() methods. the rest of type's new members are for advanced and
somewhat esoteric scenarios beyond the scope of this article. as its name indicates,
hasgenericparameters is set to true if the type represented by the type object uses generic type
parameters. getgenericparameters() returns an array of types corresponding to the bounded types
used. getgenerictypedefinition() returns a type representing the generic form of the underlying
type. hasunboundgenericparameters is true if the type object has any unspecified generic type
parameters. it follows that hasunboundgenericparameters is set to true on the type returned from
getgenerictypedefinition() when called on a generic type.
code block 10 demonstrates using these new type members to obtain generic reflection information on
the linkedlist from code block 3.
code block 10. using type for generic reflection
linkedlist<int,string> list = new linkedlist<int,string>();
type boundedtype = list.gettype();
trace.writeline(boundedtype.tostring());
//writes 'linkedlist[system.int32,system.string]'
debug.assert(boundedtype.hasgenericparameters);
type[] parameters = boundedtype.getgenericparameters();
debug.assert(parameters.length == 2);
debug.assert(parameters[0] == typeof(int));
debug.assert(parameters[1] == typeof(string));
type unboundedtype = boundedtype.getgenerictypedefinition();
debug.assert(unboundedtype.hasunboundgenericparameters);
trace.writeline(unboundedtype.tostring());
//writes 'linkedlist[k,t]'
as shown in code block 10, a type can refer to a generic type with bounded parameters
(boundedtype in code block 10), or to a generic type with unbounded parameters (unboundedtype
in code block 10).
similar to type, methodinfo and its base class methodbase have new members that reflect generic
method information.
as in c# 1.1, you can use methodinfo (as well as a number of other options) for late binding invocation.
however, the type of the parameters you pass for the late binding must match the bounded types used

本文关键:An Introduction to C# Generics
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top