zoukankan      html  css  js  c++  java
  • ylbtech-LanguageSamples-Libraries(库)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Libraries(库)

     

    1.A,示例(Sample) 返回顶部

    “库”示例

    本示例演示如何用 C# 中创建和使用 DLL。

    安全说明

    提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任。

    在 Visual Studio 中生成并运行“库”示例

    1. 在“解决方案资源管理器”中,右击“FunctionTest”项目并单击“设为启动项目”。

    2. 在“解决方案资源管理器”中,右击“FunctionTest”项目并单击“属性”。

    3. 打开“配置属性”文件夹并单击“调试”。

    4. 在“命令行参数”属性中,输入 3 5 10

    5. 单击“确定”。

    6. 在“调试”菜单中,单击“开始执行(不调试)”。这将自动在“Functions”目录中生成库并执行程序。

    从命令行生成并运行“库”示例

    1. 使用“更改目录”命令转到“Functions”目录。

    2. 键入以下命令:

      csc /target:library /out:Functions.dll Factorial.cs DigitCounter.cs
    3. 使用“更改目录”命令转到“FunctionTest”目录。

    4. 键入以下命令:

      copy ..FunctionsFunctions.dll .
      csc /out:FunctionTest.exe /R:Functions.DLL FunctionClient.cs
      FunctionTest 3 5 10
    1.B,示例代码(Sample Code)返回顶部

    1.B.1, DigiCounter.cs

    // 版权所有(C) Microsoft Corporation。保留所有权利。
    // 此代码的发布遵从
    // Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
    //
    //版权所有(C) Microsoft Corporation。保留所有权利。
    
    // DigitCounter.cs
    // 编译时使用:/target:library
    using System; 
    
    // 声明与 Factorial.cs 中的命名空间相同的命名空间。这样仅允许将 
    // 类型添加到同一个命名空间中。
    namespace Functions 
    {
        public class DigitCount 
        {
            // NumberOfDigits 静态方法计算
            // 传递的字符串中数字字符的数目:
            public static int NumberOfDigits(string theString) 
            {
                int count = 0; 
                for ( int i = 0; i < theString.Length; i++ ) 
                {
                    if ( Char.IsDigit(theString[i]) ) 
                    {
                        count++; 
                    }
                }
                return count;
            }
        }
    }
    View Code

    1.B.2, Facotrial.cs

    // 版权所有(C) Microsoft Corporation。保留所有权利。
    // 此代码的发布遵从
    // Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
    //
    //版权所有(C) Microsoft Corporation。保留所有权利。
    
    // Factorial.cs
    // 编译时使用:/target:library
    using System; 
    
    // 声明命名空间。需要根据
    // 库的命名空间打包库,以便 .NET 运行时可以正确地加载类。
    namespace Functions 
    { 
        public class Factorial 
        { 
    // “Calc”静态方法为传入的指定整数
    // 计算阶乘值:
            public static int Calc(int i) 
            { 
                return((i <= 1) ? 1 : (i * Calc(i-1))); 
            } 
        }
    }
    View Code

    1.B.3,

    1.B,示例代码2(Sample Code)返回顶部

    1.B.1, FunctionClient.cs

    // 版权所有(C) Microsoft Corporation。保留所有权利。
    // 此代码的发布遵从
    // Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
    //
    //版权所有(C) Microsoft Corporation。保留所有权利。
    
    // FunctionClient.cs
    // 编译时使用:/reference:DigitCounter.dll;Factorial.dll
    // 参数:3 5 10
    using System; 
    // 下面的 using 指令使 Functions
    // 命名空间中定义的类型可用于此编译单元:
    using Functions;
    class FunctionClient 
    { 
        public static void Main(string[] args) 
        { 
            Console.WriteLine("Function Client"); 
    
            if ( args.Length == 0 ) 
            {
                Console.WriteLine("Usage: FunctionTest ... "); 
                return; 
            } 
    
            for ( int i = 0; i < args.Length; i++ ) 
            { 
                int num = Int32.Parse(args[i]); 
                Console.WriteLine(
                   "The Digit Count for String [{0}] is [{1}]", 
                   args[i], 
                   // 调用 DigitCount 类中的
                   // NumberOfDigits 静态方法:
                   DigitCount.NumberOfDigits(args[i])); 
                Console.WriteLine(
                   "The Factorial for [{0}] is [{1}]", 
                    num,
                   // 调用 Factorial 类中的 Calc 静态方法:
                    Factorial.Calc(num) ); 
            } 
        } 
    }
    View Code

    1.B.2,

    1.C,下载地址(Free Download)返回顶部
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    C#基础—string等类的有趣方法_1
    设计模式
    OOP-面向对象程序设计
    CSS3实用效果大全
    HTML5 DOM元素类名相关操作API classList简介(转载自张鑫旭大神)
    Js写的一个倒计时效果实例
    垂直居中的几种方案
    大图片加载优化解决方案
    DomReady实现策略
    脱离文档流
  • 原文地址:https://www.cnblogs.com/ylbtech/p/4197036.html
Copyright © 2011-2022 走看看