Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize object with CodeDOM?

Tags:

c#

codedom

I need to initialize my object like below:

var obj = new EduBranch {
    Id = model.Id,
    WorklevelId = model.WorklevelId,
    EdulevelId = model.EdulevelId,
    Title = model.Title,
    StartEduYearId = model.StartEduYearId,
    EndEduYearId = model.EndEduYearId,
};

but in CodeDOM I could only find:

var objectCreate1 = new CodeObjectCreateExpression("EduBranch", 
    new CodeExpression[]{});  

and this is using parentheses to initialize instead of brackets. is there a CodeDOM approach for this? ( I already make my code using stringBuilder but I'm looking for a CodeDOM approach ) thanks

like image 363
ePezhman Avatar asked Oct 13 '12 15:10

ePezhman


1 Answers

The CodeDom doesn't currently support object initializers (or collection initializers, for that matter).

The CodeDom has fallen out of favor over the years, because of other technologies (T4 templates, for example).

That said, because this line:

var obj = new EduBranch {
    Id = model.Id,
    WorklevelId = model.WorklevelId,
    EdulevelId = model.EdulevelId,
    Title = model.Title,
    StartEduYearId = model.StartEduYearId,
    EndEduYearId = model.EndEduYearId,
};

Is effectively the same as:

var obj = new EduBranch();

obj.Id = model.Id;
obj.WorklevelId = model.WorklevelId;
obj.EdulevelId = model.EdulevelId;
obj.Title = model.Title;
obj.StartEduYearId = model.StartEduYearId;
obj.EndEduYearId = model.EndEduYearId;

You can use the CodeDOM to generate the above, and get the same

like image 73
casperOne Avatar answered Oct 13 '22 10:10

casperOne