开发者

Any reason for this call to Activator.CreateInstance?

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?


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).


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; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜