簡體   English   中英

在Visual Studio中將其他名稱空間導入為項目默認值

[英]Import additional namespace as project default in Visual Studio

有沒有一種方法可以添加到您在Visual Studio中選擇類模板時添加的默認名稱空間?

我的問題是添加一個新類(在我的情況下是C#),但不包括我的公共庫。 必須不斷地不斷添加內部自定義API的名稱空間,這對生產力產生了巨大的影響。 多年來,由於分散的問題,這個問題變得更加嚴重。 甚至.NET導入的大小也有所增加。

我在嘗試Resharper的同時提供了幫助,但不能完全解決問題。 我仍然需要具有確切的類型名稱,然后才能告訴它自動導入。

我也嘗試使用代碼片段,雖然很不錯,但是創建一個代碼片段也不是一件容易的事,每個新文件都需要額外點擊幾下。

創建新模板也很麻煩。 更不用說我們使用從控制器,視圖到基本類,接口等的各種模板。我們還需要每個模板的幾個版本,因為每個模塊通常需要2-3個其他名稱空間。 然后,我們需要在每次發布主要框架時都對其進行更新。 .NET 2.0項目模板沒有LINQ,現在在每個模板上都有。 然后,我們需要在所有Dev計算機上同步這些模板。 底線是我們沒有足夠的資源。

真的,我只想添加到默認導入(每個項目/名稱空間),而無需在其中重新創建所有主要模板。 要么使某個命名空間“本地”到給定項目,要么每次創建新類時就停止編寫一堆“ using”語句。

有沒有更簡單的方法呢?

是的,您可以更改Visual Studio在創建新類型時使用的類模板文件。 這些模板位於:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Refactoring

您的方案感興趣的模板是GenerateTypeInNewFile ,它包含以下內容:

<?xml version="1.0" encoding="utf-8"?>
    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <CodeSnippet Format="1.0.0">
            <Header>
                <Title>Generate Type In New File</Title>
                <Description>Snippet for generating a type in a new file</Description>
                <Author>Microsoft Corporation</Author>
                <SnippetTypes>
                    <SnippetType>Refactoring</SnippetType>
                </SnippetTypes>
            </Header>
            <Snippet>
                <Declarations>
                    <Literal Editable="true">
                        <ID>namespace</ID>
                        <Default></Default>
                    </Literal>
                    <Literal Editable="true">
                        <ID>modifiers</ID>
                        <Default></Default>
                    </Literal>
                    <Literal Editable="true">
                        <ID>typekind</ID>
                        <Default>class</Default>
                    </Literal>
                    <Literal Editable="true">
                        <ID>typename</ID>
                        <Default>name</Default>
                    </Literal>
                    <Literal Editable="true">
                        <ID>typebase</ID>
                        <Default></Default>
                    </Literal>
                    <Literal>
                        <ID>linqusing</ID>
                        <Function>CheckMinimumTargetFramework(3.5,
                            using System.Linq;)
                        </Function>
                    </Literal>
                </Declarations>
                <Code Language="csharp">
                    <![CDATA[$end$using System;
                        using System.Collections.Generic;$linqusing$
                        using System.Text;

                        namespace $namespace$
                        {
                            $modifiers$ $typekind$ $typename$ $typebase$
                            {
                            }
                        }
                    ]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>

您感興趣的部分是此<Code>部分:

<Code Language="csharp">
    <![CDATA[$end$using System;using System.Collections.Generic;$linqusing$
        using System.Text;

        namespace $namespace$
        {
            $modifiers$ $typekind$ $typename$ $typebase$
            {
            }
        }
    ]]>
</Code>

using System.Text;之后using xyz.blah.blah.blah添加您的using xyz.blah.blah.blah命名空間using System.Text; ,保存,您添加的下一個類應該很好。


注意:這將改變在Visual Studio中的所有項目中添加類的方式,請注意!

如果要使用ReSharper的插入文件功能添加類,則可以通過轉到ReSharper->模板管理器->文件模板來編輯使用的模板。 在C#部分下,您可以編輯“類”模板,並在頂部添加任何所需的using語句。 您可以使用宏來決定根名稱空間應該是什么,如果宏沒有給您想要的名稱,您可以編寫一個插件來添加一個宏該宏將以您想要的方式生成名稱空間:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM