VS2008生成DLL文件的方法、引用dll文件以及意义
生活随笔
收集整理的這篇文章主要介紹了
VS2008生成DLL文件的方法、引用dll文件以及意义
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一 VS2008生成dll文件的方法 ????有兩種方法: ????1:傻瓜式操作 ????打開VS2008,依次點擊:菜單->文件->新建項目->項目類型visual C#(這里假設為該項目所取的名字是DllBuild)->類庫(注意必須是類庫),即新建一個由純.cs類庫文件組成的程序集,寫好代碼之后(例如寫了一個名為DllTest.cs的類,該類的namespace取名為DllTestNS),再依次點擊:菜單->生成->生成DllBuild,這樣你的DllBuild/DllBuild/bin/Debug文件夾或者DllBuild/DllBuild/obj/Debug文件夾里便會自動生成dll文件啦,該文件名稱與項目名稱一致,即為DllBuild.dll。 ????2:使用VS命令行 ????依次點擊:開始->運行,輸入cmd,在打開的命令行窗口中輸入:cd ?\,按回車,輸入下面一行命令: ????cd c:\Program Files\Microsoft Visual Studio 8\SDK\v2.0>csc /target:library /out:d:\Pager.dll d:\Pager.cs ?????按回車,這樣,便將d:\Pager.cs 文件編譯為dll文件并保存為d:\Pager.dll。 ????在這里有可能會報錯,原因是csc.exe文件找不到。此時只需打開資源瀏覽器explorer,在“我的電腦”中搜索“csc.exe”文件即可,比如我的csc.exe文件便是在: C:\WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe。為了不至于每次編譯dll時都要輸入如此長的VS命令行路徑,我們可以將該路徑添加到系統環境變量中。具體的添加方法請見:http://hi.baidu.com/yuemingfeng/blog/item/f3bf3c24b86db46934a80fcc.html?。當添加完環境變量后,現在要將.cs文件編譯為dll文件便十分方便: ????點擊“開始”->“運行”,輸入: csc? /target:library /out:d:\Pager.dll d:\Pager.cs 這樣便直接進行編譯。 ? ????二 dll文件的引用及動態加載 ????2.1 引用dll文件 ????c++文件必須有頭文件和lib文件方能編譯通過,在運行時還必須調用相應的dll文件;而c#則直接將頭文件和lib文件都封裝進dll文件中,因此,c#編程無需再引入這兩個文件,但是在運行時或者編譯時很多時候都需要引用dll文件。??? ????在上一步,我們生成(Build)了名為DllBuild的項目,并生成了DllBuild.dll文件,現在我們重新新建一個模板類型為Console Application(控制臺應用程序)的項目,名為DllInvoke,新建好項目之后,從資源瀏覽器中打開該項目,依次打開DllInvoke\DllInvoke\bin\Debug\,將剛才生成的DllBuild.dll文件復制到Debug目錄下,同時打開DllInvoke\DllInvoke\DllInvoke.csproj文件(右擊,用記事本打開),打開后內容如下: <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
? <PropertyGroup>
??? <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
??? <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
??? <ProductVersion>8.0.50727</ProductVersion>
??? <SchemaVersion>2.0</SchemaVersion>
??? <ProjectGuid>{EE4DDE2F-AC60-4A50-A988-AB936EB00103}</ProjectGuid>
??? <OutputType>Exe</OutputType>
??? <AppDesignerFolder>Properties</AppDesignerFolder>
??? <RootNamespace>DllInvoke</RootNamespace>
??? <AssemblyName>DllInvoke</AssemblyName>
? </PropertyGroup>
? <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
??? <DebugSymbols>true</DebugSymbols>
??? <DebugType>full</DebugType>
??? <Optimize>false</Optimize>
??? <OutputPath>bin\Debug\</OutputPath>
??? <DefineConstants>DEBUG;TRACE</DefineConstants>
??? <ErrorReport>prompt</ErrorReport>
??? <WarningLevel>4</WarningLevel>
? </PropertyGroup>
? <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
??? <DebugType>pdbonly</DebugType>
??? <Optimize>true</Optimize>
??? <OutputPath>bin\Release\</OutputPath>
??? <DefineConstants>TRACE</DefineConstants>
??? <ErrorReport>prompt</ErrorReport>
??? <WarningLevel>4</WarningLevel>
? </PropertyGroup>
? <ItemGroup>
??? <Reference Include="System" />
??? <Reference Include="System.Data" />
??? <Reference Include="System.Xml" />? </ItemGroup>
? <ItemGroup>
??? <Compile Include="Program.cs" />
??? <Compile Include="Properties\AssemblyInfo.cs" />
? </ItemGroup>
? <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
? <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
?????? Other similar extension points exist, see Microsoft.Common.targets.
? <Target Name="BeforeBuild">
? </Target>
? <Target Name="AfterBuild">
? </Target>
? -->
</Project>
????可以看到,該文件主要描述了改項目的一些系統配置和屬性,例如項目名稱和根命名空間名稱、調試方式等等。由于要引用dll文件,因此我們需要在該xml格式的文件中添加關于該dll文件的描述信息,添加到<ItemGroup>節點中(即上文字體顏色為綠色的地方),添加后,該處內容變為: <ItemGroup>
??? <Reference Include="System" />
??? <Reference Include="System.Data" />
??? <Reference Include="System.Xml" />
??? <Reference Include="test, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
????? <SpecificVersion>False</SpecificVersion>
????? <HintPath>..\..\DllBuild.dll</HintPath>
??? </Reference>? </ItemGroup>
? <PropertyGroup>
??? <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
??? <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
??? <ProductVersion>8.0.50727</ProductVersion>
??? <SchemaVersion>2.0</SchemaVersion>
??? <ProjectGuid>{EE4DDE2F-AC60-4A50-A988-AB936EB00103}</ProjectGuid>
??? <OutputType>Exe</OutputType>
??? <AppDesignerFolder>Properties</AppDesignerFolder>
??? <RootNamespace>DllInvoke</RootNamespace>
??? <AssemblyName>DllInvoke</AssemblyName>
? </PropertyGroup>
? <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
??? <DebugSymbols>true</DebugSymbols>
??? <DebugType>full</DebugType>
??? <Optimize>false</Optimize>
??? <OutputPath>bin\Debug\</OutputPath>
??? <DefineConstants>DEBUG;TRACE</DefineConstants>
??? <ErrorReport>prompt</ErrorReport>
??? <WarningLevel>4</WarningLevel>
? </PropertyGroup>
? <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
??? <DebugType>pdbonly</DebugType>
??? <Optimize>true</Optimize>
??? <OutputPath>bin\Release\</OutputPath>
??? <DefineConstants>TRACE</DefineConstants>
??? <ErrorReport>prompt</ErrorReport>
??? <WarningLevel>4</WarningLevel>
? </PropertyGroup>
? <ItemGroup>
??? <Reference Include="System" />
??? <Reference Include="System.Data" />
??? <Reference Include="System.Xml" />? </ItemGroup>
? <ItemGroup>
??? <Compile Include="Program.cs" />
??? <Compile Include="Properties\AssemblyInfo.cs" />
? </ItemGroup>
? <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
? <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
?????? Other similar extension points exist, see Microsoft.Common.targets.
? <Target Name="BeforeBuild">
? </Target>
? <Target Name="AfterBuild">
? </Target>
? -->
</Project>
????可以看到,該文件主要描述了改項目的一些系統配置和屬性,例如項目名稱和根命名空間名稱、調試方式等等。由于要引用dll文件,因此我們需要在該xml格式的文件中添加關于該dll文件的描述信息,添加到<ItemGroup>節點中(即上文字體顏色為綠色的地方),添加后,該處內容變為: <ItemGroup>
??? <Reference Include="System" />
??? <Reference Include="System.Data" />
??? <Reference Include="System.Xml" />
??? <Reference Include="test, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
????? <SpecificVersion>False</SpecificVersion>
????? <HintPath>..\..\DllBuild.dll</HintPath>
??? </Reference>? </ItemGroup>
總結
以上是生活随笔為你收集整理的VS2008生成DLL文件的方法、引用dll文件以及意义的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 90后80后70后60后和50后的无奈
- 下一篇: 用例建模技巧