新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版用于讨论编程和软件设计的技巧
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 编程心得 』 → c# 反射的使用方法[原创] 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 5955 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: c# 反射的使用方法[原创] 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     jcsee007 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(高数修炼中)
      文章:4
      积分:121
      门派:XML.ORG.CN
      注册:2008/10/6

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jcsee007发送一个短消息 把jcsee007加入好友 查看jcsee007的个人资料 搜索jcsee007在『 编程心得 』 的所有贴子 引用回复这个贴子 回复这个贴子 查看jcsee007的博客楼主
    发贴心情 c# 反射的使用方法[原创]

    c# 反射的使用方法

    在网上查找了不少的资料,可以说大同小异,概念性的东西网上一搜一堆,今天把

    反射的东西整理了一下,供大家使用,我保证我这里是最全面的东西,当然也是基

    础的东西,在学好了这一切的基础上,大家可以学习反射的具体插件等应用,老鸟

    就不用看了。首先我们建立一个类库,将它生成为HelloWorld.dll,
        using System;

         namespace Webtest
         ...{

            public interface interface1
             ...{
                  int add();

             }
             public class ReflectTest:interface1
             ...{

                 public String Write;
                 private String Writec;

                 public String Writea
                 ...{
                     get
                     ...{
                         return Write;
                     }
                     set
                     ...{
                         Write = value;
                     }

                 }

                 private String Writeb
                 ...{
                     get
                     ...{
                         return Writec;
                     }
                     set
                     ...{
                         Writec = value;
                     }

                 }

                  public ReflectTest()
                  ...{
                      this.Write = "Write";
                      this.Writec = "Writec";
                  }

                 public ReflectTest(string str1,string str2)
                 ...{
                     this.Write = str1;
                     this.Writec = str2;

             }

                 public string WriteString(string s,int b)
                 ...{
                     return "欢迎您," + s + "---" + b; ;
                 }

                  public static string WriteName(string s)
                  ...{
                     return "欢迎您光临," + s;
                  }

                 public string WriteNoPara()
                 ...{
                    return "您使用的是无参数方法";
                 }

                 private string WritePrivate()
                 ...{
                     return "私有类型的方法";
                 }


                 public int add()
                 ...{
                     return 5;
                 }
             }
        }然后,建立再建立一个项目引入该HelloWorld.dll,
        using System;

        using System.Threading;
        using System.Reflection;


        class Test
        ...{
            delegate string TestDelegate(string value,int value1);

           static void Main()
            ...{
                //Assembly t = Assembly.LoadFrom("HelloWorld.dll"); 与下面相

    同的效果
                Assembly t = Assembly.Load("HelloWorld");


               foreach (Type aaa in t.GetTypes())
               ...{
                    //Console.Write(aaa.Name);   //显示该dll下所有的类
                }


                Module[] modules = t.GetModules();

                foreach (Module module in modules)
                ...{
                    //Console.WriteLine("module name:" + module.Name);//显示

    模块的名字本例为"HelloWorld.dll"
                }


                Type a = typeof(Webtest.ReflectTest);//得到具体的类的类型,和

    下面一个效果

                //Type a = t.GetType("Webtest.ReflectTest");//
                //Console.Write(a.Name);


                string[] bb =...{ "aaaa", "bbbbb" };
                object obj = Activator.CreateInstance(a,bb); //创建该类的实

    例,后面的bb为有参构造函数的参数
                //object obj = t.CreateInstance("Webtest.ReflectTest");//与

    上面方法相同


                MethodInfo[] miArr = a.GetMethods();
                foreach (MethodInfo mi0 in miArr)
               ...{
                    //Console.Write(mi0.Name);  //显示所有的共有方法
               }


                MethodInfo mi = a.GetMethod("WriteString");//显示具体的方法
                object[] aa=...{"使用的是带有参数的非静态方法",2};
                string s = (string)mi.Invoke(obj,aa); //带参数方法的调用

                MethodInfo mi1 = a.GetMethod("WriteName");
                String[] aa1 =...{"使用的是静态方法"};
                string s1 = (string)mi1.Invoke(null, aa1); //静态方法的调用

                MethodInfo mi2 = a.GetMethod("WriteNoPara");
                string s2 = (string)mi2.Invoke(obj, null); //不带参数的方法

    调用

                MethodInfo mi3 =

    a.GetMethod("WritePrivate",BindingFlags.Instance |

    BindingFlags.NonPublic);
                string s3 = (string)mi3.Invoke(obj, null); //私有类型方法调

                //Console.Write(s3);


                PropertyInfo[] piArr = a.GetProperties(BindingFlags.Instance

    | BindingFlags.NonPublic | BindingFlags.Public);
                foreach (PropertyInfo pi in piArr)
                ...{
                 //Console.Write(pi.Name);  //显示所有的方法
                }


                PropertyInfo pi1=a.GetProperty("Writea");
                //pi1.SetValue(obj, "Writea", null);
                //Console.Write(pi1.GetValue(obj,null));

                        PropertyInfo pi2 = a.GetProperty("Writeb",

    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                pi2.SetValue(obj, "Writeb", null);
                //Console.Write(pi2.GetValue(obj, null));

                FieldInfo fi1 = a.GetField("Write");
                //Console.Write(fi1.GetValue(obj));


                ConstructorInfo[] ci1 = a.GetConstructors();
                foreach (ConstructorInfo ci in ci1)
                ...{
                    //Console.Write(ci.ToString()); //获得构造函数的形式
                }

                ConstructorInfo asCI = a.GetConstructor(new Type[] ...{

    typeof(string), typeof(string) });
                //Console.Write(asCI.ToString());


                Webtest.interface1 obj1 =

    (Webtest.interface1)t.CreateInstance("Webtest.ReflectTest");
                Webtest.ReflectTest obj2 =

    (Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest");
                //Console.Write(obj1.add());典型的工厂模式


                foreach (Type tt in t.GetTypes())
                ...{
                    if (tt.GetInterface("interface1")!=null)
                    ...{
                        Webtest.interface1 obj3 =

    (Webtest.interface1)Activator.CreateInstance(a);
                        //Console.Write(obj3.add());
                    }
                }


                TestDelegate method =

    (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj,

    "WriteString");
                 //动态创建委托的简单例子
                Console.Write(method("str1", 2));
                Console.Read();
            }
        }
        在这里我把我们常用的方法,属性,等全部整理了出来,大家不要嫌弃乱,静下心

    来,自己按照我的分隔一部分一部分的来,保证你对反射的学习,会事半功倍.当然有

    关于其方法我会继续补充,想了这么些就先写下来吧.

    转自:http://www.gdglc.com/bbs/dispbbs.asp?boardid=35&Id=20141
    欢迎到www.gdglc.com留言


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/10/10 10:08:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 编程心得 』 的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/21 21:46:17

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    78.125ms