反射

反射基本用法,取自MSDN,

 1 None.gif using  System;
 2 None.gif using  System.Reflection;
 3 None.gif using  System.Security.Permissions;
 4 None.gif
 5 None.gif[assembly:AssemblyVersionAttribute( " 1.0.2000.0 " )]
 6 None.gif
 7 None.gif public   class  Example
 8 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 9InBlock.gif    private int factor;
10InBlock.gif    public Example(int f)
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12InBlock.gif        factor = f;
13ExpandedSubBlockEnd.gif    }

14InBlock.gif
15InBlock.gif    public int SampleMethod(int x) 
16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
17InBlock.gif        Console.WriteLine("\nExample.SampleMethod({0}) executes.", x);
18InBlock.gif        return x * factor;
19ExpandedSubBlockEnd.gif    }

20InBlock.gif
21InBlock.gif    public static void Main()
22ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
23InBlock.gif        Assembly assem = Assembly.GetExecutingAssembly();
24InBlock.gif
25InBlock.gif        Console.WriteLine("Assembly Full Name:");
26InBlock.gif        Console.WriteLine(assem.FullName);
27InBlock.gif
28InBlock.gif        // The AssemblyName type can be used to parse the full name.
29InBlock.gif        AssemblyName assemName = assem.GetName();
30InBlock.gif        Console.WriteLine("\nName: {0}", assemName.Name);
31InBlock.gif        Console.WriteLine("Version: {0}.{1}"
32InBlock.gif            assemName.Version.Major, assemName.Version.Minor);
33InBlock.gif
34InBlock.gif        Console.WriteLine("\nAssembly CodeBase:");
35InBlock.gif        Console.WriteLine(assem.CodeBase);
36InBlock.gif
37InBlock.gif        // Create an object from the assembly, passing in the correct number
38InBlock.gif        // and type of arguments for the constructor.
39InBlock.gif        Object o = assem.CreateInstance("Example"false
40InBlock.gif            BindingFlags.ExactBinding, 
41ExpandedSubBlockStart.gifContractedSubBlock.gif            nullnew Object[] dot.gif2 }nullnull);
42InBlock.gif
43InBlock.gif        // Make a late-bound call to an instance method of the object.    
44InBlock.gif        MethodInfo m = assem.GetType("Example").GetMethod("SampleMethod");
45ExpandedSubBlockStart.gifContractedSubBlock.gif        Object ret = m.Invoke(o, new Object[] dot.gif42 });
46InBlock.gif        Console.WriteLine("SampleMethod returned {0}.", ret);
47InBlock.gif
48InBlock.gif        Console.WriteLine("\nAssembly entry point:");
49InBlock.gif        Console.WriteLine(assem.EntryPoint);
50ExpandedSubBlockEnd.gif    }

51ExpandedBlockEnd.gif}

52 None.gif
53 ExpandedBlockStart.gifContractedBlock.gif /**/ /* This code example produces output similar to the following:
54InBlock.gif
55InBlock.gifAssembly Full Name:
56InBlock.gifsource, Version=1.0.2000.0, Culture=neutral, PublicKeyToken=null
57InBlock.gif
58InBlock.gifName: source
59InBlock.gifVersion: 1.0
60InBlock.gif
61InBlock.gifAssembly CodeBase:
62InBlock.giffile:///C:/sdtree/AssemblyClass/cs/source.exe
63InBlock.gif
64InBlock.gifExample.SampleMethod(42) executes.
65InBlock.gifSampleMethod returned 84.
66InBlock.gif
67InBlock.gifAssembly entry point:
68InBlock.gifVoid Main()
69ExpandedBlockEnd.gif */

转载于:https://www.cnblogs.com/eric-huang/archive/2008/04/15/1154483.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值