C#反射

反射技术
我经常在博客园看到运用反射技术的***,可是遗憾自己一直不知道什么是反射,急忙找些资料学习学习。
程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。反射通常具有以下用途:
使用 Assembly 定义和加载程序集,加载在程序集清单中列出的模块,以及从此程序集中查找类型并创建该类型的实例。
使用 Module 了解如下的类似信息:包含模块的程序集以及模块中的类等。您还可以获取在模块上定义的所有全局方法或其他特定的非全局方法。
使 用 ConstructorInfo 了解如下的类似信息:构造函数的名称、参数、访问修饰符(如 public 或 private)和实现详细信息(如 abstract 或 virtual)等。使用 Type 的 GetConstructors 或 GetConstructor 方法来调用特定的构造函数。
使用 MethodInfo 来了解如下的类似信息:方法的名称、返回类型、参数、访问修饰符(如 public 或 private)和实现详细信息(如 abstract 或 virtual)等。使用 Type 的 GetMethods 或 GetMethod 方法来调用特定的方法。
使用 FieldInfo 来了解如下的类似信息:字段的名称、访问修饰符(如 public 或 private)和实现详细信息(如 static)等;并获取或设置字段值。
使用 EventInfo 来了解如下的类似信息:事件的名称、事件处理程序数据类型、自定义属性、声明类型和反射类型等;并添加或移除事件处理程序。
使用 PropertyInfo 来了解如下的类似信息:属性的名称、数据类型、声明类型、反射类型和只读或可写状态等;并获取或设置属性值。
使用 ParameterInfo 来了解如下的类似信息:参数的名称、数据类型、参数是输入参数还是输出参数,以及参数在方法签名中的位置等。
举例说明:

using  System;

using  System.Reflection;



namespace  ReflectionExample

{

     
class Class1

     
{

         [STAThread]

         
static void Main(string[] args)

         
{

              Console.WriteLine(
"列出程序集中所有类型");

              Assembly a 
= Assembly.LoadFrom("ReflectionExample.exe");

              Type[] mytypes 
= a.GetTypes();

              
foreach(Type t in mytypes)

              
{

                   Console.WriteLine(t.Name);

              }


              Console.WriteLine(
"列出HelloWorld中的所有方法");

              Type ht 
= typeof(HelloWorld);

              MethodInfo[] mif 
= ht.GetMethods();

              
foreach(MethodInfo mf in mif)

              
{

                   Console.WriteLine(mf.Name);

              }


              Console.WriteLine(
"实例化HelloWorld,并调用HelloWorld方法");

              Object obj 
= Activator.CreateInstance(ht);

              
string[] s = {"wssmax"};

              Object objName 
= Activator.CreateInstance(ht,s);

              MethodInfo msayhello 
= ht.GetMethod("SayHello");

              msayhello.Invoke(obj,
null);

              msayhello.Invoke(objName,
null);

              Console.ReadLine();

         }


     }


}




using  System;



namespace  ReflectionExample

{

     
public class HelloWorld

     
{

         
string myName;

         
public HelloWorld(string name)

         
{

              myName 
= name;

         }


         
public HelloWorld():this(null)

         
{}

         
public string Name

         
{

              
get{return myName;}

         }


         
public void SayHello()

         
{

              
if(myName == null)

                   Console.WriteLine(
"Hello World");

              
else

                   Console.WriteLine(
"Hello "+myName);

         }


     }


}


得到double类的信息:

using  System;
using  System.Text;
using  System.Windows.Forms;
using  System.Reflection;

namespace  Wrox.ProCSharp.TypeView
{
    
class MainClass
    
{
        
static void Main()
        
{
            
// modify this line to retrieve details of any
            
// other data type
            Type t = typeof(double);
   
            AnalyzeType(t);
            MessageBox.Show(OutputText.ToString(), 
"Analysis of type " + t.Name);
            Console.ReadLine();
        }


        
static void AnalyzeType(Type t)
        
{
            AddToOutput(
"Type Name:  " + t.Name);
            AddToOutput(
"Full Name:  " + t.FullName);
            AddToOutput(
"Namespace:  " + t.Namespace);
            Type tBase 
= t.BaseType;
            
if (tBase != null)
                AddToOutput(
"Base Type:" + tBase.Name);
            Type tUnderlyingSystem 
= t.UnderlyingSystemType;
            
if (tUnderlyingSystem != null)
                AddToOutput(
"UnderlyingSystem Type:" + tUnderlyingSystem.Name);

            AddToOutput(
"/nPUBLIC MEMBERS:");
            MemberInfo [] Members 
= t.GetMembers();
            
foreach (MemberInfo NextMember in Members)
            
{
                AddToOutput(NextMember.DeclaringType 
+ " " + NextMember.MemberType +
                    
" " + NextMember.Name);
            }

        }


        
static void AddToOutput(string Text)
        
{
            OutputText.Append(
"/n" + Text);
        }


        
static StringBuilder OutputText = new StringBuilder(500);
    }

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值