开发者

Any reason for this call to Activator.CreateInstance?

开发者 https://www.devze.com 2023-04-07 01:24 出处:网络
I\'m maintaining someone else\'s code and they have this section in a method: object ReportCriteriaInstance =

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

精彩评论

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

关注公众号