Question

I'm maintaining someone else's code and they have this section in a method:

object ReportCriteriaInstance =
        Activator.CreateInstance(
                typeof(MyCompany.Utils.ReportStructure.ReportSearchCriteria));

//ReportCriteria is passed in as a method parameter
ReportCriteriaInstance = ReportCriteria; 

I don't know why they're setting ReportCriteriaInstace to a different value one line after instantiating it with CreateInstance().

Aside from that, because we're passing in a known type to CreateInstance (MyCompany.Utils.ReportStructure.ReportSearchCriteria) is there any reason not to use new() instead? Some default, parameterless constructor reason I'm not getting maybe?

Was it helpful?

Solution

This seems like an abandoned effort to implement poor man's DI container. Later on the object was just passed in, so the code can be safely removed (unless there is a default ReportSearchCriteria constructor that has some potential side effects).

OTHER TIPS

You can easily convert code to the following, avoiding side effects of the refactorings entirely:

var ReportSearchCriteriaInstance = new MyCompany.Utils.ReportStructure.ReportSearchCriteria();
object ReportCriteriaInstance = ReportCriteria; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top