2016-03-27 214 views
0

在我的项目中,我需要创建很多(约100)非常类似的变量。有没有模板样式或我可以使用的东西?c#如何创建大量类似的变量

private double _num; 
public double Num 
{ 
    get { return _num; } 
    set 
    { 
     if (value != _num) 
     { 
      _num = value; 
      DisplayNum = _num; 
     } 
    } 
} 

private Brush _numColor = Brushes.Black; 
public Brush NumColor 
{ 
    get { return _numColor; } 
    set 
    { 
     if (value != _numColor) 
     { 
      _numColor = value; 
      RaisePropertyChanged("NumColor"); 
     } 
    } 
} 

private double _displayNum; 
public double DisplayNum 
{ 
    get { return _displayNum; } 
    set 
    { 
     if (value != _displayNum) 
     { 
      _displayNum = value; 
      RaisePropertyChanged("DisplayNum"); 
     } 
    } 
} 

我知道我可以使用数组等,但这不是我想要的。

我想做一些像 “创建新的字符串统计”,它会创建上述但在编译时使用“stat”而不是“num”和“string”而不是“double”创建。

任何想法?

+0

您正在寻找代码片段。但是,你可能不应该那样做。你可能需要'ICustomTypeDescriptor'。 – SLaks

+0

谢谢。我会研究这些。不知道我想要的是什么适当的术语。 –

回答

0

代码片段的确是要走的路,这里是一个我写了INPC性质:

<?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>Code snippet for property and backing field with change notification</Description> 
      <Author>Mark Feldman</Author> 
      <SnippetTypes> 
       <SnippetType>Expansion</SnippetType> 
      </SnippetTypes> 
     </Header> 
     <Snippet> 
      <Declarations> 
       <Literal> 
        <ID>type</ID> 
        <ToolTip>Property type</ToolTip> 
        <Default>int</Default> 
       </Literal> 
       <Literal> 
        <ID>property</ID> 
        <ToolTip>Property name</ToolTip> 
        <Default>MyProperty</Default> 
       </Literal> 
      </Declarations> 
      <Code Language="csharp"><![CDATA[private $type$ _$property$; 
     public $type$ $property$ 
     { 
      get { return this._$property$;} 
      set { this._$property$ = value; RaisePropertyChanged(); } 
     } 
     $end$]]> 
      </Code> 
     </Snippet> 
    </CodeSnippet> 
</CodeSnippets> 

我结合这“propc”,所以我必须是那种类型DevStudio的,按Tab键然后根据需要填写每个字段。

+0

谢谢。我查看了片段,并将它们计算出来。很好,解决了我的问题。 –