Laden...

Oft benötigte Snippets im VS-Format (Event , IDisposable, ...)

Erstellt von TripleX vor 14 Jahren Letzter Beitrag vor 14 Jahren 3.733 Views
TripleX Themenstarter:in
328 Beiträge seit 2006
vor 14 Jahren
Oft benötigte Snippets im VS-Format (Event , IDisposable, ...)

hallo Gemeinde, ich habe mir demletzt einige Codesnippets gebastelt, und wollte diese euch natürlich nicht vorenthalten (habe im Forum nach einem Sammelthread oder ähnliches gesucht, bin aber nicht fündig geworden).

Singelton-Pattern (singleton)
Implementiert (fast) das Singleton-pattern. Der private Konstruktor muss noch erstellt werden.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet" >
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Singleton Snippet</Title>
      <Shortcut>singleton</Shortcut>
      <Description>Code snippet for implementing the Singleton Pattern (Threadsafe)</Description>
      <Author>David Teck</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="false">
          <ID>TypeObject</ID>
          <Function>SimpleTypeName(System.Object)</Function>
        </Literal>
        <Literal>
          <ID>classname</ID>
          <ToolTip>Class name</ToolTip>
          <Function>ClassName()</Function>
          <Default>ClassNamePlaceHolder</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
<![CDATA[$end$#region Singleton
        private static volatile $classname$ _instance;
        private static object _lock = new $TypeObject$();
        /// <summary>
        /// Get the unique instance of <see cref="$classname$"/>.
        /// This property is thread-safe.
        /// </summary>
        public static $classname$ Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null) _instance = new $classname$();
                    }
                }
                return _instance;
            }
        }
        #endregion]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

NotifyProperty (notprop)
Erstellt eine Property, welche Änderungen - über meine Implementierung von INotifyPropertyChanged (siehe weiter unten) - bekannt gibt.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet" >
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>NotifyProperty Snippet</Title>
      <Shortcut>notprop</Shortcut>
      <Description>Code snippet for a Property which notifies updates with INotifyPropertyChanged</Description>
      <Author>David Teck</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>type</ID>
          <ToolTip>The type of the property</ToolTip>
          <Default>bool</Default>
        </Literal>
        <Literal>
          <ID>fieldname</ID>
          <ToolTip>The name of the property</ToolTip>
          <Default>name</Default>
        </Literal>
        <Literal>
          <ID>propname</ID>
          <ToolTip>The name of the property</ToolTip>
          <Default>Name</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
<![CDATA[private $type$ $fieldname$;
        public $type$ $propname$
        {
            get { return $fieldname$; }
            set
            {
                if (value != $fieldname$)
                {
                    $fieldname$ = value;
                    NotifyPropertyChanged("$propname$");
                }
            }
        }$end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

INotifyPropertyChang(ed/ing) Implementierung (inotitfy):
Implementiert das INotifyPropertyChanged-Interface oder das INotifyPropertyChanging-Interface

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet" >
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>INotifyPropertyChang(ed/ing) Snippet</Title>
      <Shortcut>inotify</Shortcut>
      <Description>Code snippet for implementing INotifyPropertyChang(ed/ing)</Description>
      <Author>David Teck</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>event</ID>
          <ToolTip>This specifies the type of the event</ToolTip>
          <Default>ed</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
<![CDATA[$end$#region INotifyPropertyChang$event$ Implementation
        /// <summary>
        /// Declares the <see cref="PropertyChang$event$"> event
        /// </summary>
        public event PropertyChang$event$EventHandler PropertyChang$event$;
        /// <summary>
        /// This method will raise the <see cref="PropertyChang$event$"> event passing the
        /// source property that is being updated.
        /// </summary>
        /// <param name="propertyName">Name of the chang$event$ property.</param>
        public void NotifyPropertyChang$event$(String propertyName)
        {
            NotifyPropertyChang$event$(new PropertyChang$event$EventArgs(propertyName));
        }
        /// <summary>
        /// This method will raise the <see cref="PropertyChang$event$"> event passing the
        /// source properties that are being updated.
        /// </summary>
        /// <param name="propertyNames">Name of the chang$event$ properties.</param>
        public void NotifyPropertyChang$event$(String[] propertyNames)
        {
            foreach(string prop in propertyNames)
                NotifyPropertyChang$event$(new PropertyChang$event$EventArgs(prop));
        }
        /// <summary>
        /// This method will raise the <see cref="PropertyChang$event$"> event passing the
        /// source property that is being updated.
        /// </summary>
        /// <param name="args"><see cref="PropertyChang$event$EventArgs"/> of the chang$event$ property.</param>
        public void NotifyPropertyChang$event$(PropertyChang$event$EventArgs args)
        {
            if (PropertyChang$event$ != null)
            {
                PropertyChang$event$(this, args);
            }
        }
        #endregion]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

IDisposable Implementierung (idisposable):
Implementiert das IDisposable-Interface

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet" >
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>IDisposeable Snippet</Title>
      <Shortcut>idisposable</Shortcut>
      <Description>Code snippet for implementing IDisposeable</Description>
      <Author>David Teck</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>classname</ID>
          <ToolTip>Class name</ToolTip>
          <Function>ClassName()</Function>
          <Default>ClassNamePlaceHolder</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
<![CDATA[$end$#region IDisposable Implemetation
        ~$classname$ ()
        {
           Dispose (false);
        }
        /// <summary>
        /// Disposes the <see cref="$classname$"/>
        /// </summary>
        public void Close ()
        {
           ((IDisposable)this).Dispose ();
        }
        /// <summary>
        /// Closes and disposes the <see cref="$classname$"/>
        /// </summary>
        void IDisposable.Dispose ()
        {
           Dispose (true);
           GC.SuppressFinalize (this);
        }
        /// <summary>
        /// This method is called by <see cref="Dispose()"/>. 
        /// Derived classes can override this method.
        /// </summary>
        /// <param name="cleanManagedResources">
        /// <para><see langword="true"/>: Disposes managed and unmanaged resources</para>
        /// <para><see langword="false"/>: Disposes only unmanaged resources</para>
        /// </param>
        protected virtual void Dispose(bool cleanManagedResources)
        {
            if (cleanManagedResources)
            {
                // Clean up the managed resources
            }
            // Clean up the unmanaged resources
        }
        #endregion]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Event (event)
Erstellt ein simples event

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet" >
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Event Snippet</Title>
      <Shortcut>event</Shortcut>
      <Description>Code snippet for creating an event</Description>
      <Author>David Teck</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>name</ID>
          <ToolTip>Specify the name of the event</ToolTip>
          <Default>MyEvent</Default>
        </Literal>
        <Literal>
          <ID>comment</ID>
          <ToolTip>Specify the XML-Comment for the event</ToolTip>
          <Default></Default>
        </Literal>
        <Literal>
          <ID>access</ID>
          <ToolTip>Specify the accessibility the event</ToolTip>
          <Default>public</Default>
        </Literal>
        <Literal>
          <ID>handler</ID>
          <ToolTip>Specify the EventHandler for the event</ToolTip>
          <Default>EventHandler</Default>
        </Literal>
        <Literal>
          <ID>param</ID>
          <ToolTip>Specifies the parameter for the event</ToolTip>
          <Default>EventArgs.Empty</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
<![CDATA[$end$#region $name$-Event
        /// <summary>
        /// Occurs when $comment$
        /// </summary>
        $access$ event $handler$ $name$;
        private void On$name$()
        {
            if ($name$ != null) $name$(this, $param$);
        }
        #endregion]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

SimpleCommand (mvvm_simcom)
_Erstellt einen neuen SimpleCommand (Command für WPF) _

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet" >
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>MVVMFrameWork - SimpleCommand Snippet</Title>
      <Shortcut>mvvm_simcom</Shortcut>
      <Description>Code snippet for a SimpleCommands</Description>
      <Author>David Teck</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Imports>
        <Import>
          <Namespace>MVVMFrameWork.Commands</Namespace>
        </Import>
      </Imports>
      <Declarations>
        <Literal>
          <ID>fieldname</ID>
          <ToolTip>The name of the command</ToolTip>
          <Default>name</Default>
        </Literal>
        <Literal>
          <ID>propname</ID>
          <ToolTip>The name of the command</ToolTip>
          <Default>Name</Default>
        </Literal>
        <Literal>
          <ID>cancode</ID>
          <ToolTip>The code which indicates if the command can be executed</ToolTip>
          <Default>true</Default>
        </Literal>
        <Literal>
          <ID>docode</ID>
          <ToolTip>The code which will be executed</ToolTip>
          <Default></Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
<![CDATA[$end$#region $propname$Command
        private SimpleCommand $fieldname$Command;
        public SimpleCommand $propname$Command
        {
            get {
                if ($fieldname$Command == null)
                    $fieldname$Command = new SimpleCommand(x => Execute$propname$Command(), x => CanExecute$propname$Command);
                return $fieldname$Command;
            }
        }
        /// <summary>
        /// Called, when the <see cref="$propname$Command"/> should be executed.
        /// </summary>
        private void Execute$propname$Command()
        {
            $docode$
        }
        /// <summary>
        /// <see langword="true"/> if the <see cref="$propname$Command"/> can be executed.
        /// </summary>
        private bool CanExecute$propname$Command
        {
            get
            {
                return $cancode$;
            }
        }
        #endregion]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Falls ihr Verbesserungsvorschläge oder andere Snippets habt, welche ihr austauschen möchtet, dann postet sie doch bitte hier 😃

Schlagwörter: Singleton, INotifyPropertyChanged, IDisposable, SimpleCommnd, Snippet

Träume nicht dein Leben sondern lebe deinen Traum.
Viele Grüße, David Teck