您现在的位置是:主页 > news > 外贸电商网站设计/交换友链

外贸电商网站设计/交换友链

admin2025/6/10 17:46:50news

简介外贸电商网站设计,交换友链,seo快速排名分析,基于 的企业网站建设[转]反射 - 原文:http://blog.csdn.net/xiaolei1982/archive/2008/04/15/2294364.aspx 看了下文章,很具参考性,我干脆就拷贝下来了。 在网上查找了不少的资料,可以说大同小异,概念性的东西网上一搜一堆, 今…

外贸电商网站设计,交换友链,seo快速排名分析,基于 的企业网站建设[转]反射 - 原文:http://blog.csdn.net/xiaolei1982/archive/2008/04/15/2294364.aspx 看了下文章,很具参考性,我干脆就拷贝下来了。 在网上查找了不少的资料,可以说大同小异,概念性的东西网上一搜一堆, 今…
[转]反射
-

原文:http://blog.csdn.net/xiaolei1982/archive/2008/04/15/2294364.aspx

看了下文章,很具参考性,我干脆就拷贝下来了。

 

在网上查找了不少的资料,可以说大同小异,概念性的东西网上一搜一堆,
今天把反射的东西整理了一下,供大家使用,我保证我这里是最全面的东西,当然也是基础的东西,
在学好了这一切的基础上,大家可以学习反射的具体插件等应用,老鸟就不用看了.
首先我们建立一个类库,将它生成为HelloWorld.dll,

ContractedBlock.gifExpandedBlockStart.gifCode
using System;

namespace Webtest
ExpandedBlockStart.gifContractedBlock.gif
{
    
public interface interface1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
int add();
    }


    
public class ReflectTest : interface1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public String Write;
        
private String Writec;

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


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


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


        
public ReflectTest(string str1, string str2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.Write = str1;
            
this.Writec = str2;
        }


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


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


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


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


        
public int add()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return 5;
        }

    }

}

然后,建立再建立一个项目引入该HelloWorld.dll,

ContractedBlock.gifExpandedBlockStart.gifCode
using System;
using System.Threading;
using System.Reflection;

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

    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//Assembly t = Assembly.LoadFrom("HelloWorld.dll"); 与下面相同的效果
        Assembly t = Assembly.Load("HelloWorld");

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


        
//**********************************************************************
        Module[] modules = t.GetModules();

        
foreach (Module module in modules)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//Console.WriteLine("module name:" + module.Name);//显示模块的名字本例为"HelloWorld.dll"
        }


        
//**********************************************************************
        Type a = typeof(Webtest.ReflectTest);//得到具体的类的类型,和下面一个效果
        
//Type a = t.GetType("Webtest.ReflectTest");//
        
//Console.Write(a.Name);

        
//**********************************************************************
ExpandedSubBlockStart.gifContractedSubBlock.gif
        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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//Console.Write(mi0.Name);  //显示所有的共有方法
        }


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

        MethodInfo mi1 
= a.GetMethod("WriteName");
ExpandedSubBlockStart.gifContractedSubBlock.gif        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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//Console.Write(ci.ToString()); //获得构造函数的形式
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        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())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (tt.GetInterface("interface1"!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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);

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

        Console.Read();
    }

}


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

posted on 2009-02-04 15:42 Jrong 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/Jrong/archive/2009/02/04/1383938.html