zoukankan      html  css  js  c++  java
  • Assembly 类

    下面的代码示例演示如何获取当前执行的程序集,如何创建该程序集中包含的某个类型的实例以及如何用后期绑定调用该类型的方法之一。 为此,该代码示例定义了一个名为Example 的类,该类具有一个名为 SampleMethod 的方法。 该类的构造函数接受整数,用于计算方法的返回值。

    该代码示例还演示如何使用 GetName 方法来获取可用于分析程序集的全名的 AssemblyName 对象。 该示例显示程序集的版本号、CodeBase 属性和 EntryPoint 属性。

    using System;
    using System.Reflection;
    using System.Security.Permissions;

    namespace ConsoleApplication1
    {

    [assembly: AssemblyVersionAttribute("1.0.2000.0")]

    public class Example
    {
    private int factor;
    public Example(int f)
    {
    factor = f;
    }

    public int SampleMethod(int x)
    {
    Console.WriteLine(" Example.SampleMethod({0}) executes.", x);
    return x * factor;
    }

    public static void Main()
    {
    Assembly assem = Assembly.GetExecutingAssembly();

    Console.WriteLine("Assembly Full Name:");
    Console.WriteLine(assem.FullName);

    // The AssemblyName type can be used to parse the full name.
    AssemblyName assemName = assem.GetName();
    Console.WriteLine(" Name: {0}", assemName.Name);
    Console.WriteLine("Version: {0}.{1}",
    assemName.Version.Major, assemName.Version.Minor);

    Console.WriteLine(" Assembly CodeBase:");
    Console.WriteLine(assem.CodeBase);

    // Create an object from the assembly, passing in the correct number
    // and type of arguments for the constructor.
    // 加上命名空间
    Object o = assem.CreateInstance("ConsoleApplication1.Example", false,
    BindingFlags.ExactBinding,
    null, new Object[] { 2 }, null, null);

    // Make a late-bound call to an instance method of the object.
    MethodInfo m = assem.GetType(" ConsoleApplication1.Example").GetMethod("SampleMethod"); //加上命名空间

    Object ret = m.Invoke(o, new Object[] { 42 });
    Console.WriteLine("SampleMethod returned {0}.", ret);

    Console.WriteLine(" Assembly entry point:");
    Console.WriteLine(assem.EntryPoint);
    Console.ReadKey();
    }
    }

    }

  • 相关阅读:
    几个前端时间插件总结
    微信支付——退款篇
    getTime()方法在苹果系统的bug
    【转载】[JS]让表单提交返回后保持在原来提交的位置上
    【转载】 IE/Firefox每次刷新时自动检查网页更新,无需手动清空缓存的设置方法
    webstorm相关设置
    检测无标题的
    数组去重的方法
    Git 版本比较
    Git 回到过去
  • 原文地址:https://www.cnblogs.com/netmvc8/p/3514193.html
Copyright © 2011-2022 走看看