反射初步
通过学习反射了解反射应用的场景有如下几个方面: 动态生成类型实例、动态调用成员(方法/属性)、动态指定参数、后期加载、绑定、工厂模式。
主要的成员有:
Assembly、Type、MemberInfo、EventInfo、ParameterInfo、FieldInfo等。我们可以通过foreach语句来迭代**Info中的具体值。例如:
当然需要System.Reflection命名空间。还有MSDN中一个非常好的代码例子:ms-help://MS.MSDNQTR.v90.chs/fxref_mscorlib/html/7e1337f8-5ea2-2a48-8a74-37315ae74f0a.htm
主要的成员有:
Assembly、Type、MemberInfo、EventInfo、ParameterInfo、FieldInfo等。我们可以通过foreach语句来迭代**Info中的具体值。例如:
//Type stringType = typeof(string);
//Console.WriteLine("The public constructor of string are:");
//foreach (ConstructorInfo item in stringType.GetConstructors())
//{
// if (item!=null)
// {
// Console.WriteLine(item.ToString());
// }
当然需要System.Reflection命名空间。还有MSDN中一个非常好的代码例子:ms-help://MS.MSDNQTR.v90.chs/fxref_mscorlib/html/7e1337f8-5ea2-2a48-8a74-37315ae74f0a.htm