VS2008生成VS2005样式的Property

在vs 2008中通过输入porp ,自动生成的代码段不是vs2005 中的格式,而是  public int MyProperty { get; set; }
这种定义方式是c# 3.0的新特性:自动属性(Automatic Properties),但在.net 2.0的项目中不支持自动属性。因此我们需要新建一个代码段。
1 新建一个propc.snippet文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  
<CodeSnippet Format="1.0.0">
    
<Header>
      
<Title>propc</Title>
      
<Shortcut>propc</Shortcut>
      
<Description>自动实现的属性的代码段</Description>
      
<Author>Microsoft Corporation</Author>
      
<SnippetTypes>
        
<SnippetType>Expansion</SnippetType>
      
</SnippetTypes>
    
</Header>
    
<Snippet>
      
<Declarations>
        
<Literal>
          
<ID>type</ID>
          
<ToolTip>属性类型</ToolTip>
          
<Default>int</Default>
        
</Literal>
        
<Literal>
          
<ID>property</ID>
          
<ToolTip>属性名</ToolTip>
          
<Default>MyProperty</Default>
        
</Literal>
        
<Literal>
          
<ID>field</ID>
          
<ToolTip>The variable backing this property</ToolTip>
          
<Default>myVar</Default>
        
</Literal>
      
</Declarations>
      
<Code Language="csharp">
        
<![CDATA[private $type$ $field$; 
    public $type$ $property$ 
    { 
        get { return $field$;} 
        set { $field$ = value;} 
    } 
    $end$
]]>
      
</Code>
    
</Snippet>
  
</CodeSnippet>
</CodeSnippets>

2 拷贝此文件到:
C:\Documents and Settings\[User]\My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets目录中
或者
你的VS2008安装目录\Microsoft Visual Studio 9.0\VC#\Snippets\2052\Visual C# 目录中

如果操作正确,但是在vs2008中并没有出现此代码段,原因是上面内容中的中文字符由于编码的原因,变成了乱码,手动修改一下就好了!

 


posted @ 2009-10-24 12:33  闭着眼睛想  阅读(504)  评论(0编辑  收藏  举报