¿Hay alguna forma, en tiempo de diseño, de inicializar un objeto con todas las propiedades en Visual Studio 2010?

Antecedentes

I am writing code in VS 2010, .NET 4, C#. Also, in case it matters, I am using the latest version of ReSharper.

Pregunta

Digamos que tengo este modelo:

public class SomeObject
        {
            public string Red{ get; set; }
            public string Green{ get; set; }
            public string Blue{ get; set; }
            public string Yellow{ get; set; }
            public string Purple{ get; set; }
            public string Orange{ get; set; }
            public string Black{ get; set; }
        }

Elsewhere in the code, I need to instantiate one of these objects, like so:

SomeObject myObject = new SomeObject{
                                     red = "some value", 
                                     blue = "some other value",
                                     . . .,
                                     black="last value"
                                    };

*NOTA: *I will sometimes want to initialize this object with only a subset of its total possible properties (i.e. just red and blue).

At the moment, I am having to type in each property (red, blue, green, etc) for each new instance of SomeObject. Is there a hotkey or something in VS2010 to have those properties pre-populated so I just have to assign values to each rather than typing each one?

preguntado el 31 de enero de 12 a las 16:01

prepopulated with what value? The default for the type? Like what happens if you don't actually set them (unless you've overridden the default constructor)? -

This is what a constructor is for. -

@Ramhound unless you aren't the one who wrote the constructor -

8 Respuestas

You can use Smart Code Completion.

Write: new SomeObject{

Then press Ctrl+Alt+Space: -> All possible properties are listed in a tooltip window

Then select a property (i.e. 'Red') and press ENTER -> new SomeObject{Red = }

Then type in "," and press again Ctrl+Alt+Space, select next property and so on.

Respondido 06 Feb 12, 13:02

At the moment, I am having to type in each property (red, blue, green, etc) for each new instance of SomeObject.

Personally, if you need to set these for every object instance, I would handle this by adding a constructor that takes all of the parameters, and just not use the object initialization syntax. This will give you full intellisense, but also help/force you to construct the object correctamente Cada vez

Remember, a type's constructor debemos force you to supply all parameters required to correctly initialize the object to a proper state. The one downside to object initializers is that they don't force this at compile time. As such, if all of these properties are son requeridos for each object instance, the constructor should supply them instead of using object initializers.

Respondido el 31 de enero de 12 a las 20:01

Except with a constructor, you are forced to pass in every parameter, even if you only want to populate, for example, red and yellow. Unless you overload the constructor for all the possible combinations. It depends on what your usage is. - Escocés

@Scottie Yes - But the OP specifically stated " I am having to type in each property (red, blue, green, etc) for each new instance of SomeObject." In this case, it sounds like a constructor is the right way to go. - Reed Copsey

Good morning to you all and thanks for the thoughts. REED: A constructor sounds like the correcta approach; however, as Scottie stated, there are times when I only want to initialize SomeObject with a subset of its total possible properties. I should have been more clear about that in the OP, but at any rate, is this bad practice? Thanks! - Matt Cashatt

@MatthewPatrickCashatt You can have more than one constructor. Your constructor(s) should always put the objects into a valid state - so at least force the minimum required properties to be set by the constructors. I would have as many constructors as it takes so I'm always making "correct" objects (without object initializers), then using object initializers to set additional properties as needed. - Reed Copsey

Note that this is FAR easier in C# 4, too, since you can use optional and named arguments in your constructors. You can have a constructor with default values, and override as necessary - even using named arguments to override in special cases. - Reed Copsey

Are all the properties always the same? Why not add a constructor where they are already set? Or more than one.

Respondido el 31 de enero de 12 a las 20:01

Take a look at Code Snippets. I think this will give you what you want.

Respondido el 31 de enero de 12 a las 20:01

While using a Code Snippet that allows him to continue doing the thing he is doing, that doesn't mean what he is doing is right, using a class constructor is a far better idea. - Sabueso de seguridad

@Ramhound unless, of course, you can't write the constructor - drzaus

There is no keyboard shortcut to generically prepare an object initializer.

The VS IDE or Reshaper can not make a sensible guess about the values you would like to set the properties to, so would have to insert invalid or, at best, sub optimal code.

You could implement a specific solution, as the other answers suggest, and constructrs are proabably the way to go if you find yourself doing this repititiously but, I don't believe that was your question.

Respondido el 31 de enero de 12 a las 21:01

Actually ReSharper already does something similar when generating new properties -- it creates a template with "highlighted" edit points for the value type, name and ??? placeholders for the getter/setter code, so there's "no reason" it can't do the same when exploding instance initialization. - drzaus

Do you really want the colors to be read/write? If not, this is the simplest solution:

public class SomeObject
{ 
    public const string Red = "red";
    public const string Green = "green";
    public const string Blue = "blue";
    ...
}

However, if you want to be more flexible, and be able initialize only some of the properties, you can use the object initialization syntax. Using your original SomeObject implementation you can create a new object with:

var obj = new SomeObject { Red = "red", Blue = "blue" };

Respondido el 31 de enero de 12 a las 21:01

I have created a VS macro that does this. My main usage is to generate dummy test data for my DTOs.

You write a line like:

var x = new SomeType();

The macro will then generate a line for each writable property/member like

x.IntProperty = 0;
x.StringProperty = "StringProperty";
x.DateProperty = new DateTime(2012, 1, 31);
x.SomeIntList = new List<int>();
x.SomeTypeProperty = new SomeType();

(and you can of course rerun the macro on the last line to generate x.SomeTypeProperty.Y = 0 etc.)

Option Explicit On
Option Strict On
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text.RegularExpressions
Imports System.Windows.Forms

Friend Module InitializeAllMembers
    Public DTE As EnvDTE80.DTE2
    Private ReadOnly _usings As New HashSet(Of String)

    Sub InitializeAllMembers(ByVal getValue As Boolean)
        Try
            Dim memberAssignment As Func(Of String, String, String, EnvDTE.CodeTypeRef, String) = Nothing
            Dim selection As EnvDTE.TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
            Dim editPoint As EditPoint = selection.BottomPoint.CreateEditPoint

            editPoint.StartOfLine()

            Dim className As String = ""
            Dim indent As String = ""
            Dim variable As String = ""
            Dim parseError As String

            If DTE.ActiveDocument.ProjectItem Is Nothing Then
                MessageBox.Show("File does not belong to a project")
            End If

            If DTE.ActiveDocument.Language = "Basic" Then
                parseError = GetVbInitialization(editPoint.GetText(editPoint.LineLength), className, indent, variable)
                If getValue Then
                    memberAssignment = AddressOf VbMemberGetValue
                Else
                    memberAssignment = AddressOf VbMemberAssignment
                End If
            ElseIf DTE.ActiveDocument.Language = "CSharp" Then
                parseError = GetCSharpInitialization(editPoint.GetText(editPoint.LineLength), className, indent, variable)
                If getValue Then
                    memberAssignment = AddressOf CSharpMemberGetValue
                Else
                    memberAssignment = AddressOf CSharpMemberAssignment
                End If
            Else
                parseError = "Not supported language " & DTE.ActiveDocument.Language
            End If

            If parseError IsNot Nothing Then
                MessageBox.Show(parseError)
                Exit Sub
            End If

            Dim currentFunction As CodeElement = FindCodeElement(selection.ActivePoint, DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElements)
            If currentFunction Is Nothing Then
                MessageBox.Show("Can't find current function")
                Exit Sub
            End If
            _usings.Clear()
            FindAllUsings(currentFunction)

            Dim classType As CodeElement = DTE.Solution.Projects.Cast(Of Project) _
                                                                .Select(Function(x) FindClassInProjectItems(x.ProjectItems, className)) _
                                                                .FirstOrDefault(Function(x) x IsNot Nothing)

            If classType Is Nothing Then
                MessageBox.Show("Can't find type in solution: " & className)
                Exit Sub
            End If

            DTE.UndoContext.Open("InitializeAllMembers")
            PrintMemberAssignments(memberAssignment, editPoint, indent, variable, GetMembers(classType))
            If TypeOf classType Is CodeClass Then
                For Each base As CodeElement In CType(classType, CodeClass).Bases
                    If TypeOf base Is CodeClass Then
                        PrintMemberAssignments(memberAssignment, editPoint, indent, variable, GetMembers(base))
                    End If
                Next
            End If
            DTE.UndoContext.Close()
        Catch objException As System.Exception
            MessageBox.Show(objException.Message)
        End Try
    End Sub

    Private Function GetVbInitialization(ByVal line As String, _
                                         ByRef className As String, _
                                         ByRef indent As String, _
                                         ByRef variable As String) As String
        Dim vbinitialization As New Regex("(?<Indent>\s*)Dim\s+(?<VariableName>[\S=]+)\s+(?:As\s+(?:New\s+)?(?<DeclaredType>[^\s\(]+))?(?:\s*=\s*New\s+(?<CreatedType>[^\s\(]+))?", _
                                          RegexOptions.IgnoreCase)
        Dim match As Match = vbinitialization.Match(line)
        If Not match.Success Then
            Return "No assignment on row"
        End If
        Dim foundDeclaredType As Boolean = match.Groups("DeclaredType").Success
        Dim foundCreatedType As Boolean = match.Groups("CreatedType").Success
        If Not (foundDeclaredType OrElse foundCreatedType) Then
            Return "Can't find type on row"
        End If
        className = If(foundDeclaredType, match.Groups("DeclaredType"), match.Groups("CreatedType")).Value
        indent = match.Groups("Indent").Value
        variable = match.Groups("VariableName").Value
        Return Nothing
    End Function

    Private Function GetCSharpInitialization(ByVal line As String, _
                                             ByRef className As String, _
                                             ByRef indent As String, _
                                             ByRef variable As String) As String
        Dim csharpinitialization As New Regex("(?<Indent>\s*)(?:(?<DeclaredType>\S+)\s+)?(?<VariableName>[\S=]+)\s*=\s*(?<new>new)?\s*(?<CreatedType>[^\s\(]+)")
        Dim match As Match = csharpinitialization.Match(line)
        If Not match.Success Then
            Return "No assignment on row"
        End If
        Dim foundDeclaredType As Boolean = match.Groups("DeclaredType").Success AndAlso match.Groups("DeclaredType").Value <> "var"
        Dim foundCreatedType As Boolean = match.Groups("new").Success
        If Not (foundDeclaredType OrElse foundCreatedType) Then
            Return "Can't find type on row"
        End If
        className = If(foundDeclaredType, match.Groups("DeclaredType"), match.Groups("CreatedType")).Value
        indent = match.Groups("Indent").Value
        variable = match.Groups("VariableName").Value
        Return Nothing
    End Function

    Sub FindAllUsings(ByVal elem As Object)
        If TypeOf elem Is CodeFunction Then
            FindAllUsings(CType(elem, CodeFunction).Parent)
        ElseIf TypeOf elem Is CodeClass Then
            _usings.Add(CType(elem, CodeClass).FullName)
            FindAllUsings(CType(elem, CodeClass).Parent)
        ElseIf TypeOf elem Is CodeStruct Then
            _usings.Add(CType(elem, CodeStruct).FullName)
            FindAllUsings(CType(elem, CodeStruct).Parent)
        ElseIf TypeOf elem Is CodeNamespace Then
            _usings.Add(CType(elem, CodeNamespace).FullName)
            For Each ns As String In CType(elem, CodeNamespace).Members.OfType(Of CodeImport) _
                                                                       .Select(Function(x) x.Namespace)
                _usings.Add(ns)
            Next
            FindAllUsings(CType(elem, CodeNamespace).Parent)
        ElseIf TypeOf elem Is FileCodeModel Then
            For Each ns As String In CType(elem, FileCodeModel).CodeElements.OfType(Of CodeImport) _
                                                                            .Select(Function(x) x.Namespace)
                _usings.Add(ns)
            Next
        End If
    End Sub

    Public Function FindCodeElement(ByVal caretPosition As TextPoint, ByVal elems As CodeElements) As CodeElement
        If elems Is Nothing Then Return Nothing
        Return elems.Cast(Of CodeElement) _
                    .Where(Function(x) x.StartPoint.LessThan(caretPosition) AndAlso _
                                       x.EndPoint.GreaterThan(caretPosition)) _
                    .Select(Function(x) If(FindCodeElement(caretPosition, GetMembers(x)), x)) _
                    .FirstOrDefault()
    End Function

    Public Sub PrintMemberAssignments(ByVal memberAssignment As Func(Of String, String, String, CodeTypeRef, String), _
                                      ByVal editPoint As EditPoint, _
                                      ByVal indent As String, _
                                      ByVal variable As String, _
                                      ByVal members As CodeElements)
        For Each member As CodeElement In members
            Dim typeref As EnvDTE.CodeTypeRef
            If TypeOf member Is CodeProperty2 Then
                Dim prop As CodeProperty2 = CType(member, CodeProperty2)
                If prop.Setter Is Nothing Then
                    If prop.Access <> vsCMAccess.vsCMAccessPublic Then Continue For
                    If prop.ReadWrite = vsCMPropertyKind.vsCMPropertyKindReadOnly Then Continue For
                    If prop.IsShared Then Continue For
                ElseIf prop.Setter.Access <> vsCMAccess.vsCMAccessPublic Then
                    Continue For
                ElseIf prop.Setter.IsShared Then
                    Continue For
                End If
                typeref = prop.Type
            ElseIf TypeOf member Is CodeVariable Then
                Dim var As CodeVariable = CType(member, CodeVariable)
                If var.Access <> vsCMAccess.vsCMAccessPublic Then Continue For
                If var.IsConstant Then Continue For
                If var.IsShared Then Continue For
                typeref = var.Type
            Else
                Continue For
            End If

            editPoint.EndOfLine()
            editPoint.Insert(ControlChars.NewLine)
            editPoint.Insert(memberAssignment(indent, variable, member.Name, typeref))
        Next
    End Sub

    Private Function TrimKnownNamespace(ByVal fullName As String) As String
        Return fullName.Substring(_usings.Where(Function(x) fullName.StartsWith(x) AndAlso _
                                                            fullName.Length > x.Length AndAlso _
                                                            fullName(x.Length) = "."c) _
                                     .Select(Function(x) x.Length + 1) _
                                     .DefaultIfEmpty(0) _
                                     .Max())
    End Function

    Private Function FindClassInProjectItems(ByVal nprojectItems As ProjectItems, ByVal classname As String) As CodeElement
        If nprojectItems Is Nothing Then Return Nothing
        For Each nprojectitem As ProjectItem In nprojectItems
            Dim found As CodeElement
            If nprojectitem.Kind = EnvDTE.Constants.vsProjectItemKindPhysicalFile Then
                If nprojectitem.FileCodeModel Is Nothing Then Continue For
                found = FindClassInCodeElements(nprojectitem.FileCodeModel.CodeElements, classname)
                If found IsNot Nothing Then Return found
            End If
            If nprojectitem.SubProject IsNot Nothing Then
                found = FindClassInProjectItems(nprojectitem.SubProject.ProjectItems, classname)
                If found IsNot Nothing Then Return found
            End If
            found = FindClassInProjectItems(nprojectitem.ProjectItems, classname)
            If found IsNot Nothing Then Return found
        Next
        Return Nothing
    End Function

    Private Function FindClassInCodeElements(ByVal elems As CodeElements, ByVal classname As String) As CodeElement
        If elems Is Nothing Then Return Nothing
        For Each elem As CodeElement In elems
            If IsClassType(elem) Then
                If classname = elem.Name Then Return elem
            ElseIf Not TypeOf elem Is CodeNamespace Then
                Continue For
            End If
            If _usings.Contains(elem.FullName) Then
                Dim found As CodeElement = FindClassInCodeElements(GetMembers(elem), classname)
                If found IsNot Nothing Then Return found
            End If
        Next
        Return Nothing
    End Function

    Private Function GetMembers(ByVal elem As CodeElement) As CodeElements
        If TypeOf elem Is CodeClass Then
            Return CType(elem, CodeClass).Members
        ElseIf TypeOf elem Is CodeNamespace Then
            Return CType(elem, CodeNamespace).Members
        ElseIf TypeOf elem Is CodeStruct Then
            Return CType(elem, CodeStruct).Members
        ElseIf TypeOf elem Is CodeInterface Then
            Return CType(elem, CodeInterface).Members
        End If
        Return Nothing
    End Function

    Private Function IsClassType(ByVal elem As CodeElement) As Boolean
        Return TypeOf elem Is CodeClass OrElse TypeOf elem Is CodeStruct OrElse TypeOf elem Is CodeInterface
    End Function

    Private Function CSharpMemberAssignment(ByVal indent As String, _
                                            ByVal variable As String, _
                                            ByVal membername As String, _
                                            ByVal typeref As EnvDTE.CodeTypeRef) As String
        Dim typekind As EnvDTE.vsCMTypeRef = typeref.TypeKind
        Dim value As String
        If typekind = vsCMTypeRef.vsCMTypeRefArray Then
            value = "{0}{1}.{2} = new {3}[1];"
            Return String.Format(value, indent, variable, membername, TrimKnownNamespace(typeref.ElementType.AsString)) & _
                   ControlChars.NewLine & _
                   CSharpMemberAssignment(indent, variable, membername & "[0]", typeref.ElementType)
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefBool Then
            value = "false"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefChar Then
            value = "'x'"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefDecimal Then
            value = "0.00m"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefDouble Then
            value = "0.00"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefInt Then
            value = "0"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefLong Then
            value = "0"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefShort Then
            value = "0"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefByte Then
            value = "0"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefString Then
            value = """" & membername & """"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefCodeType AndAlso _
                typeref.AsString = "System.DateTime" Then
            value = String.Format("new DateTime({0:yyyy}, {0:%M}, {0:%d})", DateTime.Today)
        Else
            value = "new " & TrimKnownNamespace(typeref.AsString) & "()"
        End If
        Return String.Format("{0}{1}.{2} = {3};", indent, variable, membername, value)
    End Function

    Private Function CSharpMemberGetValue(ByVal indent As String, _
                                          ByVal variable As String, _
                                          ByVal membername As String, _
                                          ByVal typeref As EnvDTE.CodeTypeRef) As String
        Dim typekind As EnvDTE.vsCMTypeRef = typeref.TypeKind
        Dim value As String
        If typekind = vsCMTypeRef.vsCMTypeRefArray Then
            value = "{0}{1}.{2} = new {3}[1];"
            Return String.Format(value, indent, variable, membername, TrimKnownNamespace(typeref.ElementType.AsString)) & _
                   ControlChars.NewLine & _
                   CSharpMemberGetValue(indent, variable, membername & "[0]", typeref.ElementType)
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefBool Then
            value = "src.GetBoolean()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefChar Then
            value = "src.GetChar()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefDecimal Then
            value = "src.GetDecimal()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefDouble Then
            value = "src.GetDouble()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefInt Then
            value = "src.GetInt32()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefLong Then
            value = "src.GetInt64()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefShort Then
            value = "src.GetInt16()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefByte Then
            value = "src.GetByte()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefString Then
            value = "src.GetString()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefCodeType AndAlso _
                typeref.AsString = "System.DateTime" Then
            value = "src.GetDateTime()"
        Else
            value = "new " & TrimKnownNamespace(typeref.AsString) & "()"
        End If
        Return String.Format("{0}{1}.{2} = {3};", indent, variable, membername, value)
    End Function

    Private Function VbMemberAssignment(ByVal indent As String, _
                                        ByVal variable As String, _
                                        ByVal membername As String, _
                                        ByVal typeref As EnvDTE.CodeTypeRef) As String
        Dim typekind As EnvDTE.vsCMTypeRef = typeref.TypeKind
        Dim value As String
        If typekind = vsCMTypeRef.vsCMTypeRefArray Then
            value = "{0}Redim {1}.{2}(1)" ' Vb don't need type argument
            Return String.Format(value, indent, variable, membername, TrimKnownNamespace(typeref.ElementType.AsString)) & _
                   ControlChars.NewLine & _
                   VbMemberAssignment(indent, variable, membername & "(0)", typeref.ElementType)
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefBool Then
            value = "False"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefChar Then
            value = """x""c"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefDecimal Then
            value = "0.00d"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefDouble Then
            value = "0.00r"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefInt Then
            value = "0"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefLong Then
            value = "0L"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefShort Then
            value = "0s"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefByte Then
            value = "0 AS Byte"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefString Then
            value = """" & membername & """"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefArray Then
            value = "New " & TrimKnownNamespace(typeref.ElementType.AsString) & "(1)"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefCodeType AndAlso _
                typeref.AsString = "Date" Then
            value = String.Format("new Date({0:yyyy}, {0:%M}, {0:%d})", DateTime.Today)
        Else
            value = "new " & TrimKnownNamespace(typeref.AsString) & "()"
        End If
        Return String.Format("{0}{1}.{2} = {3}", indent, variable, membername, value)
    End Function

    Private Function VbMemberGetValue(ByVal indent As String, _
                                        ByVal variable As String, _
                                        ByVal membername As String, _
                                        ByVal typeref As EnvDTE.CodeTypeRef) As String
        Dim typekind As EnvDTE.vsCMTypeRef = typeref.TypeKind
        Dim value As String
        If typekind = vsCMTypeRef.vsCMTypeRefArray Then
            value = "{0}Redim {1}.{2}(1)" ' Vb don't need type argument
            Return String.Format(value, indent, variable, membername, TrimKnownNamespace(typeref.ElementType.AsString)) & _
                   ControlChars.NewLine & _
                   VbMemberGetValue(indent, variable, membername & "(0)", typeref.ElementType)
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefBool Then
            value = "src.GetBoolean()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefChar Then
            value = "src.GetChar()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefDecimal Then
            value = "src.GetDecimal()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefDouble Then
            value = "src.GetDouble()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefInt Then
            value = "src.GetInt32()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefLong Then
            value = "src.GetInt64()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefShort Then
            value = "src.GetInt16()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefByte Then
            value = "src.GetByte()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefString Then
            value = "src.GetString()"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefArray Then
            value = "New " & TrimKnownNamespace(typeref.ElementType.AsString) & "(1)"
        ElseIf typekind = vsCMTypeRef.vsCMTypeRefCodeType AndAlso _
                typeref.AsString = "Date" Then
            value = "src.GetDateTime()"
        Else
            value = "new " & TrimKnownNamespace(typeref.AsString) & "()"
        End If
        Return String.Format("{0}{1}.{2} = {3}", indent, variable, membername, value)
    End Function
End Module

Respondido el 02 de Septiembre de 15 a las 09:09

How do you execute the macro? - Daniel

Anyone knows how to use it? - Leszek P

Good morning you should be able to do this either by creating a Visual Studio Code Snippet o creando un ReSharper Code Template

Respondido el 31 de enero de 12 a las 20:01

Sí, pero cómo do you do it with a code template? There's a ReSharper macro for 'Execute smart completion', but that just pops up the list the same as esta respuesta, which still requires you to basically type out each one. Is there a macro to enumerate a macro result? - drzaus

Es posible que necesites make a custom macro for this, per otra respuesta - drzaus

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.