dimanche 24 janvier 2016

How to use complied type in another compile class using CodeDOM?

I'm use CodeDOM to compile class in run time.

First generate Invoice class

string code1 = @"using System;
                            namespace CodeDOM
                            {
                             public class Invoice
                             {
                              public string InvoiceNo{ get; set; }
                              public DateTime Date{ get; set; }
                              public double Value{ get; set; }
                             }
                            }"; 
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();          
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code1);

After that compile another class.This calss use Invoice class.

string code2 = @"using System;
                             namespace CodeDOM
                            {
                             public class DataInjector
                             {
                              public void Insert( CodeDOM.Invoice obj ){ 

                              }
                             }
                            }";
CSharpCodeProvider provider1 = new CSharpCodeProvider();
CompilerParameters parameters1 = new CompilerParameters();
parameters1.GenerateInMemory = false;
parameters1.GenerateExecutable = false;
CompilerResults results1 = provider1.CompileAssemblyFromSource(parameters1, code2);

Final results1 show error "The type or namespace name 'Invoice' does not exist in the namespace 'CodeDOM' (are you missing an assembly reference?)"

Why we cant use Invoice class?





Aucun commentaire:

Enregistrer un commentaire