1

I have to replace ~250 instances of object initializers with a constructor with the same parameters.

This is an example of my object initializer:

throw new FaultException<DTFaultDetail>(new DTFaultDetail
{
    IsUserWarning = true,
    HttpStatusCode = (int)HttpStatusCode.Forbidden,
    Category = (int)FaultCategory.User,
    ErrorCode = (int)FaultCode.Jobs_TimesheetOverlapsExisting,
    UserMessage = UserStrings.TimesheetOverlapsExisting,
    DebugMessage = $"",
    TicksUsedBeforeFailure = DbContext.RequestInfo.TotalTicksUsed,
    Origin = new DTFaultDetail.FaultOrigin
    {
        Session = Session,
        ThreadID = Thread.CurrentThread.ManagedThreadId,
        Source = $"{FullNameOfThisClass}.{MethodBase.GetCurrentMethod()})"
    }
});

And I had to replace it with this:

throw new FaultException<DTFaultDetail>(new DTFaultDetail(
    isUserWarning: true,
    httpStatusCode: (int)HttpStatusCode.Forbidden,
    category: (int)FaultCategory.User,
    errorCode: (int)FaultCode.Jobs_TimesheetOverlapsExisting,
    userMessage: UserStrings.TimesheetOverlapsExisting,
    debugMessage: $"",
    ticksUsedBeforeFailure: DbContext.RequestInfo.TotalTicksUsed,
    origin: new DTFaultDetail.FaultOrigin
    {
        Session = Session,
        ThreadID = Thread.CurrentThread.ManagedThreadId,
        Source = $"{FullNameOfThisClass}.{MethodBase.GetCurrentMethod()})"
    }));

How can I do that quickly, using built-in features of Visual Studio (2015)?

Besides manual replacement, of course.

Aske B.
  • 6,419
  • 8
  • 35
  • 62
  • I already did the work, so I'm just asking out of curiosity. – Aske B. Nov 10 '16 at 13:33
  • if it is always the exact same text, then mark your original text (the class and its object initialisattion) and then pres Strg+R+R and insert als replacement, the text in the second code tag – Radinator Nov 10 '16 at 13:37
  • The values after colons and equal-signs are not always the same. In fact the `errorCode` field is always unique, but they were all of the type `FaultException`. – Aske B. Nov 10 '16 at 13:39
  • you can use regular expressions in find&replace – KMoussa Nov 10 '16 at 15:08
  • here I found somthing interesting: http://stackoverflow.com/questions/29678162/how-to-convert-a-c-sharp-object-initializer-to-use-a-constructor-using-resharper – Radinator Nov 11 '16 at 06:35

0 Answers0