5

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

casperOne
  • 73,706
  • 19
  • 184
  • 253
ePezhman
  • 4,010
  • 7
  • 44
  • 80

1 Answers1

5

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

casperOne
  • 73,706
  • 19
  • 184
  • 253
  • T4 is code generation for dummies. Yes, it has it's place for small, quick projects, but there is still no substitute for a large-scale web service or enterprise application than CodeDOM. The only complaint I have about it is the lack of support for generic types. Other than that, there's just no substitute when you have real work to do. – Quark Soup Jun 11 '14 at 12:57