码迷,mamicode.com
首页 > 其他好文 > 详细

ylbtech-LanguageSamples-Libraries(库)

时间:2015-01-02 01:03:22      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

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 ..\Functions\Functions.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)返回顶部

 

技术分享 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

ylbtech-LanguageSamples-Libraries(库)

标签:

原文地址:http://www.cnblogs.com/ylbtech/p/4197036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!