views:

635

answers:

3

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

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

But rather than giving me that string value, I want the date in the format specified.

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

I'm open to not using an Intellisense Code Snippet.. I just thought that would be easy.

+1  A: 

For the GUID, you can use:

string firstOctet = System.Guid.NewGuid().ToString().Split('-')[0];
John Rasch
+1  A: 

To achieve what you're trying to do with your XML block, you're going to need to either embed a scripting language engine such as IronPython, or write your own simple engine. C# source code isn't compiled at run-time, nor is the source code evaluated at runtime. The IL bytecode is, but not the human-readable source code.

yodaj007
in Visual Studio, there's a C#-based templating language called T4 you could use.
Jimmy
+1  A: 

This is what I did instead with a 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
Rob