Compile CS source code using ICodeCompiler 
Author Message
 Compile CS source code using ICodeCompiler

Hi,

I want to compile CS source code in program. However, I always get error
"CS0116 - A namespace does not directly contain members such as fields or
methods" when I invoke CompileAssemblyFromSource method. Even if I changed
the source file name to an invalid value, the same error message will be
returned. How does this happen?

This is my code:

CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Xml.dll");
cp.ReferencedAssemblies.Add("System.Web.Services.dll");
cp.GenerateExecutable = false;
cp.OutputAssembly = "C:\\Lib\\BT20TT0.dll;
cp.GenerateInMemory = true;
cp.IncludeDebugInformation = false;

CSharpCodeProvider cscp = new CSharpCodeProvider();
ICodeCompiler icc = cscp.CreateCompiler();

CompilerResults cr = icc.CompileAssemblyFromSource(cp,
"C:\\Source\\BT20TT0.cs");

Thanks & Regards,
Weiming



Sat, 07 May 2005 14:35:52 GMT  
 Compile CS source code using ICodeCompiler

Quote:
> I want to compile CS source code in program. However, I always get error
> "CS0116 - A namespace does not directly contain members such as fields or
> methods" when I invoke CompileAssemblyFromSource method. Even if I changed
> the source file name to an invalid value, the same error message will be
> returned. How does this happen?

The source you are trying to compile must be contained in a class.  Can you
post what it is that you are trying to compile?

-- Alan



Sat, 07 May 2005 14:40:16 GMT  
 Compile CS source code using ICodeCompiler
use CompileAssemblyFromFile  instead of CompileAssemblyFromSource .....

'fromSource' is used when a string variable contains the source code of your
application ...


Quote:
> Alan,

> The source file is attached. Though I don't define a namespace in it, I
can
> compile it successfully with CSC.exe as below:

> C:\Winnt\Microsoft.NET\Framework\v1.0.3705\csc.exe /target:library
> BT20TT0.cs

> I am afraid ICodeCompiler can not read in the source file to compile as
the
> error message is the same as I compile a file which is not exist on hard
> disk. But I don't know the reason.

> Thanks & Regards,
> Weiming





> > > I want to compile CS source code in program. However, I always get
error
> > > "CS0116 - A namespace does not directly contain members such as fields
> or
> > > methods" when I invoke CompileAssemblyFromSource method. Even if I
> changed
> > > the source file name to an invalid value, the same error message will
be
> > > returned. How does this happen?

> > The source you are trying to compile must be contained in a class.  Can
> you
> > post what it is that you are trying to compile?

> > -- Alan



Sat, 07 May 2005 19:18:20 GMT  
 Compile CS source code using ICodeCompiler
Thank you for reply. It works!!!


Quote:
> use CompileAssemblyFromFile  instead of CompileAssemblyFromSource .....

> 'fromSource' is used when a string variable contains the source code of
your
> application ...



> > Alan,

> > The source file is attached. Though I don't define a namespace in it, I
> can
> > compile it successfully with CSC.exe as below:

> > C:\Winnt\Microsoft.NET\Framework\v1.0.3705\csc.exe /target:library
> > BT20TT0.cs

> > I am afraid ICodeCompiler can not read in the source file to compile as
> the
> > error message is the same as I compile a file which is not exist on hard
> > disk. But I don't know the reason.

> > Thanks & Regards,
> > Weiming





> > > > I want to compile CS source code in program. However, I always get
> error
> > > > "CS0116 - A namespace does not directly contain members such as
fields
> > or
> > > > methods" when I invoke CompileAssemblyFromSource method. Even if I
> > changed
> > > > the source file name to an invalid value, the same error message
will
> be
> > > > returned. How does this happen?

> > > The source you are trying to compile must be contained in a class.
Can
> > you
> > > post what it is that you are trying to compile?

> > > -- Alan



Sun, 08 May 2005 09:07:46 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to compile Visual C++ 1.0 source code using Visual C++ 4.0

2. cs source code

3. Lost of source code file after compiling

4. Simple question on Security in Programs compiled from C source code

5. How can re-compile to source code??

6. source code and compiling

7. Tool for source code decryption on compile time

8. can't compile the source code!

9. Compiling Unix C Source Code under DOS

10. how to compile ansi-c source code

11. Can I compile MFC source code to generate a mfc42.dll

12. Compiling cfortune.zip source code with MSC

 

 
Powered by phpBB® Forum Software