Creating my first snippet: trying to auto add a reference

余生长醉 提交于 2019-12-13 17:27:19

问题


I'm trying to create my first snippet. One of the things that I often do is to create a class wrapper object for reading a web.config or app.config file. Below is my attempt. One of the things that I wanted to include in the snippet was adding a reference to the System.configuration.dll file. When I execute the snippet, no reference is added but the snippet otherwise works.

Assuming that this is possible and that i am not misunderstanding the intent of the REFERENCE section, what am I doing wrong? What file path should I specify for this dll? Should I pick it up from the GAC? Which location should I use to best allow me to transfer this snippet to another PC?

C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a

Also, any comments you have on the subject are welcomed.

Thank you!

<?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>

Edit

I did some more digging and I saw this MS example. I decided to save the snippet code on this page to a snippet file and see if it would add the System.Data.dll referenced. Since that dll seems to be added automatically, I manually removed the reference from the project and then ran the snippet below. The snippet inserted the code, but again, no reference.

<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>

I was using the editor found here, but I see it doesn't allow one to set the URL property, so I tried another one that did, but still no luck.

来源:https://stackoverflow.com/questions/20640850/creating-my-first-snippet-trying-to-auto-add-a-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!