c# 反射

1. 普通反射

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Globalization;

namespace CSharp
{
    
public class TestReflection
    {
        
public TestReflection()
        {
            
try
            {
                Type t 
= Type.GetType("CSharp.Apple2");
                Console.WriteLine(
"t is :{0}", t.ToString());

                
object obj = System.Activator.CreateInstance(t);
                Console.WriteLine(
"t is :{0}", obj);

                
//在创建实例的时候,默认执行了对应的构造函数
                object obj2 = System.Activator.CreateInstance(t,new object[]{"sdf",2});
                Console.WriteLine();
                
//获取字段信息:
                FieldInfo[] fiList = t.GetFields();
                
                
//只能读取公共的field
                foreach (FieldInfo fi in fiList)
                {
                    Console.WriteLine(
"Name is : {0};IsPrivate :{1};IsPublic:{2};IsStatic:{3};FieldType:{4}",
                        
new object[]{fi.Name,fi.IsPrivate,fi.IsPublic,fi.IsStatic,fi.FieldType});

                    fi.SetValue(obj, 
"Hello World");
                    Console.WriteLine(
"Field {0}'s value is : {1}", fi.Name, fi.GetValue(obj));
                }
                Console.WriteLine();
                PropertyInfo[] piList 
= t.GetProperties();
                
foreach (PropertyInfo pi in piList)
                {
                    Console.WriteLine(
"Name is : {0};CanRead :{1};CanWrite:{2};PropertyType:{3}",
                       
new object[] { pi.Name,pi.CanRead,pi.CanWrite,pi.PropertyType});

                    
                    
//t.InvokeMember(pi.Name, BindingFlags.SetProperty, null, obj, new object[] { "sdf" });
                    
                    
//object o1 = System.Activator.CreateInstance(pi.PropertyType);
                    
//pi.SetValue(obj, , null);
                }

                PropertyInfo pii 
= t.GetProperty("Color");
                t.InvokeMember(pii.Name, BindingFlags.SetProperty, 
null, obj, new object[]{"I am color"});
                Console.WriteLine(
"obj.color is :{0}",pii.GetValue(obj,null));

                Console.WriteLine();
                MethodInfo[] miList 
= t.GetMethods();
                
foreach (MethodInfo _mi in miList)
                {
                    Console.WriteLine(
"Name is : {0};ReturnType :{1};MemberType:{2};",
                       
new object[] { _mi.Name,_mi.ReturnType,_mi.MemberType });

                    ParameterInfo[] paramInfoList 
= _mi.GetParameters();

                    Console.WriteLine();
                    Console.WriteLine(_mi.Name 
+ " 's params is :");

                    
foreach (ParameterInfo paramInfo in paramInfoList)
                    {

                        Console.WriteLine(
"Param Name is : {0};ParameterType :{1};Position:{2};",
                       
new object[] { paramInfo.Name, paramInfo.ParameterType,paramInfo.Position });
                    }
                }
                Console.WriteLine();
                MethodInfo mi 
= t.GetMethod("BuyApple");
                
object value = mi.Invoke(obj, new object[] { 3 });
                Console.WriteLine(
"mi.invoke BuyApple : {0}", value);

                t.InvokeMember(mi.Name, BindingFlags.InvokeMethod, 
null, obj, new object[] { 3 });
                Console.WriteLine(
"t.InvokeMember BuyApple : {0}", value);

            }
            
catch (Exception e)
            {
                Console.WriteLine(
"Exception is : {0}", e.Message);
            }

            
//Console.ReadLine();
        }
    }

    
public class Apple2
    {
        
//定义字段
        private string _color = "";
        
private decimal _weight = 0;
        
public string _test = "Hello";

        
//定义跟字段对应的属性
        public string Color
        {
            
get { return _color; }
            
set { _color = value; } //这里的value是C#关键字。表示外面传入的值.
        }

        
public decimal Weight
        {
            
get { return _weight; }
            
set { _weight = value; }
        }

        
public Apple2()
        {

        }

        
public Apple2(string s,int i)
        {
            Console.WriteLine(
"param s is :{0};param i is:{1} ",s,i);
        }

        
public string BuyApple(int nums)
        {
            
return "BuyApple";
        }

        
public override string ToString()
        {
            
return "I am Apple2";
        }
    }
}

 

2. 泛型反射

 

posted @ 2009-01-08 19:20  无尽思绪  阅读(525)  评论(0编辑  收藏  举报