Creating a new Guid inside a code snippet using C #

I want to make an intellisense code snippet using Ctl K + Ctl X that actually executes the code when it runs ... for example, I would like to do the following:

 <![CDATA[string.Format("{MM/dd/yyyy}", System.DateTime.Now);]]>

But instead of telling me this string value, I need a date in the specified format.

Another example of what I want is creating a new Guid, but truncating the first octet, so I would like to use creating a new Guid using System.Guid.NewGuid (); to give me {798400D6-7CEC-41f9-B6AA-116B926802FE}, but I want the value: 798400D6 from the code snippet.

I am open to not use the Intellisense code snippet. I just thought it would be easy.

+5
source share
3 answers

This is what I did instead with the VS macro

  Public Sub InsertPartialGuid()            
        Dim objSel As TextSelection = DTE.ActiveDocument.Selection        
        Dim NewGUID As String = String.Format("{0}", (System.Guid.NewGuid().ToString().ToUpper().Split("-")(0)))
        objSel.Insert(NewGUID, vsInsertFlags.vsInsertFlagsContainNewText)
        objSel = Nothing
    End Sub
+2
source

For a GUID, you can use:

string firstOctet = System.Guid.NewGuid().ToString().Split('-')[0];
+1
source

To achieve what you are trying to do with your XML block, you will need to either build in a scripting engine such as IronPython, or write your own simple engine. C # source code does not compile at runtime, and source code is not evaluated at runtime. The IL bytecode is, but not human readable, source code.

0
source

All Articles