我正在尝试创建我的第一个代码段。我经常做的一件事是创建一个类包装器对象来读取web.config或app.config文件。以下是我的尝试。我希望在代码段中包含的一件事是添加对System.configuration.dll文件的引用。当我执行代码段时,不会添加任何引用,但代码段会起作用。
假设这是可能的并且我没有误解参考部分的意图,我做错了什么? 我应该为这个dll指定什么文件路径?我应该从GAC拿起它吗?我应该使用哪个位置来最好地将此代码段传输到另一台PC?
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a
此外,欢迎您就此主题发表任何评论。
谢谢!
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Keywords>
<Keyword>appparam</Keyword>
</Keywords>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>appparam</Title>
<Author>Cigna</Author>
<Description>
</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>appparam</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>"C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.configuration.dll"</Assembly>
</Reference>
</References>
<Declarations>
<Literal Editable="true">
<ID>DatabaseConnectionString</ID>
<ToolTip>
</ToolTip>
<Default>
</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>StringSetting</ID>
<ToolTip>StringSetting</ToolTip>
<Default>StringSetting</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>IntegerSetting</ID>
<ToolTip>IntegerSetting</ToolTip>
<Default>IntegerSetting</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[ public static class ApplicationParameter
{
public static string $DatabaseConnectionString$
{
get
{
return ConfigurationManager.ConnectionStrings["$DatabaseConnectionString$"].ConnectionString;
}
public string $StringSetting$
{
get
{
return ConfigurationManager.AppSettings["$StringSetting$"].ToString();
}
}
public int $IntegerSetting$
{
get
{
return (int))ConfigurationManager.AppSettings["$IntegerSetting$"];
}
}
}
$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
修改
我做了更多挖掘,我看到this MS示例。我决定将此页面上的代码段代码保存到代码段文件中,看看它是否会添加引用的System.Data.dll。由于该dll似乎是自动添加的,我手动从项目中删除了引用,然后运行下面的代码片段。该代码段插入了代码,但同样没有引用。
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet>
<Header>
<!-- Add Header information here -->
</Header>
<Snippet>
<References>
<Reference>
<Assembly>System.Data.dll</Assembly>
</Reference>
</References>
<Imports>
<Import>
<Namespace>System.Data</Namespace>
</Import>
</Imports>
<!-- Add rest of code snippet here -->
</Snippet>
</CodeSnippet>
</CodeSnippets>
我使用的是here编辑器,但我发现它不允许设置网址属性,所以我尝试了another one that did,但仍然没有运气。