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

推荐订阅源

量子位
博客园_首页
罗磊的独立博客
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
Last Week in AI
Last Week in AI
D
DataBreaches.Net
Jina AI
Jina AI
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
D
Docker
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
H
Help Net Security
T
The Blog of Author Tim Ferriss

博客园 - AnsonWu

[转载]Remoting随想 [转]Remoting之同步—异步调用 [转]正则表达式30分钟入门教程 准备写一个智能提示的控件,大家有什么好的思路 javascript函数库 推荐一个免费的数据库开发工具 从WEB SERVICE 中数据压缩策略(转) Asp.net Ajax 设计模式-原型模式 设计模式--工厂方法 如何用SQLDMO在ASP.NET页面下实现数据库的备份与恢复 .NET开源项目介绍及资源推荐:数据持久层 在asp.net中如何从视频文件中抓取一桢并生成图像文件 使用AppUpdater组件(转) NHibernate初体验 <转> ASP.net的RUL重写 开发 Windows Mobile 应用程序 FAQ(转) Ajax for Java developers: Ajax with Direct Web Remoting 手机壁纸切割器源码发布
关于反射的一点理解(一)
AnsonWu · 2007-04-05 · via 博客园 - AnsonWu

反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类、结构、委托、接口和枚举等)的成员,包括方法、属性、事件,以及构造函数等。还可以获得每个成员的名称、限定符和参数等。有了反射,即可对每一个类型了如指掌。如果获得了构造函数的信息,即可直接创建对象,即使这个对象的类型在编译时还不知道。

/*
 * Created by SharpDevelop.
 * User: anson.wu
 * Date: 2007-4-5
 * Time: 11:39
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
using System;
using
 System.Reflection;
using
 System.Data;namespace ReflectionTest
{
    
class
 MainClass
    {
        
public static void Main(string
[] args)
        {
            Assembly ass;
            Type type;
            
object
 obj;
            
string libPath=@"E:\C#\ReflectLib\bin\Debug\ReflectLib.dll"
;
            
if(!
System.IO.File.Exists(libPath))            
            {
                Console.Write(
"The DLL Is Not Exist!"
);
                Console.Read();
                
return
;
            }
            
try

            {
                ass
=Assembly.LoadFrom(libPath);
                type
=ass.GetType("ReflectLib.TestMethod"
);
                System.Reflection.MethodInfo method
=type.GetMethod("GetLibVersion"
);
                obj
=ass.CreateInstance("ReflectLib.TestMethod"
);
                
//调用GetLibVersion方法

                Console.WriteLine(method.Invoke(obj,null));
                
//调用Add方法

                method=type.GetMethod("Add");
                
object[] par = new object[] {4,5
};
                Console.WriteLine(method.Invoke(obj,par));
                
//调用静态方法

                method=type.GetMethod("GetWelcome");
                Console.WriteLine(method.Invoke(
null,null
));        

            }

catch(Exception e)
            {
                Console.Write(e.Message);
            }
            
        }
    }
}

外部程序集代码

/*
 * Created by SharpDevelop.
 * User: anson.wu
 * Date: 2007-4-5
 * Time: 11:36
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
using System;
namespace
 ReflectLib
{
    
/// <summary>

    
/// Description of MyClass.
    
/// </summary>

    public class TestMethod
    {
        
public string
 GetLibVersion()
        {
            
return "Current Version 1.0.2"
;
        }
        
public int Add(int x,int
 y)
        {
            
return x+
y;
        }
        
public static string
 GetWelcome()
        {
            
return "Hello , C# Reflect!"
;
        }

    }
}