惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - 酷咖啡

收藏:精妙SQL语句 用户中心 - 博客园 博文阅读密码验证 - 博客园 web2.0配色收集 C#访问修饰符 C#中相等的判断 [LoveCherry]一步一步学Linq to sql系列文章 [LoveCherry]无废话C#设计模式系列文章 .Net常用资源收集 Some word in English about Company website Reflection,Regular Expression,Threading,IO,AppDomain,Web Service/Remoting Service,ORM 收藏:笔记本得理器型号规格 如何让虚拟目录里面的webconfig不继承网站[转] 刚刚用上了百度Hi 自定义可绑定数据的业务对象实体和强类型 Asp.net 2.0 网站首页生成静态的方法 数据库设计的5种常见关系 [收集]visual studio文件扩展名 说说最近的工作
也谈C#中的反射用法
酷咖啡 · 2008-04-15 · via 博客园 - 酷咖啡

首先我们建立一个类库,将它生成为HelloWorld.dll,

然后,建立再建立一个项目引入该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));

//**********************************************************************
        ConstructorInfo asCI1 = a.GetConstructor(new Type[0]);
        Webtest.ReflectTest obj5 
= (Webtest.ReflectTest)asCI1.Invoke(null);
            //
通过无参构造函数实例化的方法
        
//Console.Write(obj5.Writea);

        ConstructorInfo asCI2 
= a.GetConstructor(new Type[] typeof(string), typeof(string) });
          //
通过有参构造函数实例化的方法
        Webtest.ReflectTest obj6 = (Webtest.ReflectTest)asCI2.Invoke(bb);
        Console.Write(obj6.Writea);
//**********************************************************************

        Console.Read();
    }
   
}

本文把常用的方法,属性,等全部整理了出来,大家不要嫌弃乱,静下心来,自己按照我的分隔一部分一部分的来,保证你对反射的学习,会事半功倍.当然有关于其方法我会继续补充,想了这么些就先写下来吧.