Mahsa Hassankashi
Mahsa Hassankashi

Reputation: 2139

Compile at run time exclude class that is generated by CodeDom

I have used CodeDom to generate class so called "Product.cs", my class has been generated in right path but my code does not work perfect and complete because of this "Product.cs" is exclude from my solution and I have to include it at run time I searched and found way to create csc.exe and load assembly in order to compile my class at run time and include it in my solution and after that I can work on "Product.cs".

I am writing code by Visual Studio 2013 on web application such as MVC. Thank you in advanced for any helps.

Upvotes: 1

Views: 124

Answers (2)

Gaurav Arora
Gaurav Arora

Reputation: 2324

It will work for you. I just tried, see comments

  • if you want to include class files, edit the project file. This file is a xml file. You can easily edit it. To know the tag, I suggest please open the project file in any text editor like notepad, use MSBuild or whatever you want to use to build your project.
  • if you want to generate and create a project see reflection. Also, check this: http://www.codeproject.com/Articles/10324/Compiling-code-during-runtime

Upvotes: 1

Mahsa Hassankashi
Mahsa Hassankashi

Reputation: 2139

To include an exclude file particularly when it is generated by code generation methods such as CodeDom or EnvDTE and etc, should follow:

  1. Right Click on the References on the solution
  2. Select Assemblies -> Microsoft.Build
  3. Write such code:

    var p = new Microsoft.Build.Evaluation.Project(@"D:\MVCNHibernate\MVCNHibernate\MVCNHibernate.csproj");
     p.AddItem("Compile", @"D:\MVCNHibernate\MVCNHibernate\Entities\Product.cs");
     p.Save();
     p.Build();

Then "Product.cs" will be your included file on the project or solution and it is possible to use it.

Upvotes: 1

Related Questions