Click here to Skip to main content
11,928,985 members (56,384 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# compilation
I wrote the following code to create a class dynamically

StringBuilder sourcecode = new StringBuilder();
sourcecode.Append("using System;" + Environment.NewLine);
sourcecode.Append("using System.ComponentModel;" + Environment.NewLine);
sourcecode.Append("namespace testThis " + Environment.NewLine +"{");
sourcecode.Append("public class Sample { } }"); 
CompilationResult = RuntimeCompiler.CompileAssemblyFromSource(Parameters, sourcecode.ToString());

But it gives me Compiler error CS0234: The type or namespace name 'ComponentModel' does not exist in the namespace 'System' (are you missing an assembly reference?)}

To get the DLL file name I wrote
System.ComponentModel.SomeCLassName, I put the cursor on this classname and pressed F12, I got the file
string s = @ "C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.dll"
and tried to load it using
Assymbly a = Assymbly.LoadFrom(s)
before constructing the string, but it still gives me the same error.
I need some help to resolve this problem.
Posted 14-Feb-13 11:51am
ryanb31 14-Feb-13 16:55pm
Interesting. I haven't done that before but I wonder if you can't do a using statement when loading the dll dynamically.
Sergey Alexandrovich Kryukov 14-Feb-13 17:02pm
Nothing interesting. Of course it can be done, but only using a strong name, not location. Huge abuse. I'm answering, will be ready soon.
Sergey Alexandrovich Kryukov 14-Feb-13 17:08pm
I basically answered, please see.
Pretty difficult to explain thing for an inquirer on this level... :-)
Sergey Alexandrovich Kryukov 14-Feb-13 17:01pm
Very, very bad. Can I unsee it? :-)
You should read on basics first, instead of doing something you have no clue about...
Tarek Elqusi 14-Feb-13 18:54pm
Thanks for your explanation, I actually didn't have the concept of an assymbly strong name, and I got it from your reply. Thanks again
I'll write the following code segment instead

System.Reflection.Assembly assymbly = System.Reflection.Assembly.Load("System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089");

Another point, even when you described me or my question as bad, I'll step back and be good one day.I'll base my way on your guidance and the advices of other gurus I meet.
Sergey Alexandrovich Kryukov 14-Feb-13 19:19pm
Great. And you are very welcome.

Anyway, your follow-up questions will be welcome. And I hope that you understand the criticism correctly and react constructively. You already do. The question was kind of bad, but I imply that it's bad "just yet". Of course your interest and hard work will quickly bring you to a good level.

Please accept the answer(s) formally (green button) — thanks.
Again, it was not easy to put all introductory information in one answer, but you should really focus on the concept of assembly, module (subliminal concept, but the assemblies consist of executable module, only Visual Studio always compiles a project into the assembly with only one module, but the compilers can do differently), assembly name (for strong, you also need to understand assymmetric encryption), referencing, application domain, IL, CIL, JIT, and then types and members, OOP, etc. So, if you have follow-up questions closed to your original problem, please ask...

Tarek Elqusi 14-Feb-13 20:15pm
Your second reply is great, it gave me leading points to focus on to understand assemblies more deeply. On the other hand the reply of Alan was the exact one of my question.
Thank you all for your help.
Sergey Alexandrovich Kryukov 14-Feb-13 22:10pm
And why you did not accept my answer then? You can always accept any number of them.
Tarek Elqusi 15-Feb-13 8:00am
ok, I admit that I walked the wrong way by trying to reference an assembly using its location, and I accepted your solution.

But here is a point deserves some discussion. The point is that even your valuable advice I found the final key that open the closed door for me was the Alan's reply.

Based on that I suggest on the CodeProject team to add a way so we can prioritize the accepted answers based on its close to the aimed answer.
Alan N 14-Feb-13 19:13pm
During which compilation does the error occur.
Is during compilation of the code shown in your question


When that code executes the dynamic compilation CompileAssemblyFromSource?
Tarek Elqusi 14-Feb-13 19:26pm
The second choice
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Right-click on your Project and select "Add Reference". Find System.ComponentModel in the Framework list and add it or the applicable sub assembly. That should be all you need to do.

Forget trying to add a reference with the Assembly.LoadFrom stuff. Totally the wrong way to do that.
ryanb31 14-Feb-13 17:05pm
OP is trying to build a compiler though.
Marcus Kramer 14-Feb-13 17:36pm
I didn't get that out of the question. I assume that bit of information is from previous questions. Oh well....
ryanb31 14-Feb-13 20:51pm
I guess you're right. He's generating a class dynamically, I guess I added the reason why. His code is creating code.
Sergey Alexandrovich Kryukov 14-Feb-13 19:19pm
Sure, a 5. I answered in some more detail, please see...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You are doing it wrong; and this is the fundamental thing. Without it, you cannot develop any .NET products. So, you are not developing. Smile | :)

First of all, assemblies are referenced. Using Assembly.LoadFrom is the advanced topic, Reflection. Let's not discuss it here.
First, you need to understand the concept of assembly and GAC:[^],[^],[^],[^].

Assemblies in Global Assembly Cache (GAC) are referenced (and even loaded dynamically) using their strong names, not DLL location. You should never rely on exact location: the platform does not guarantee it. You are trying to use a hard-coded path name. There are no situations where hard-coded path names can be useful, never at all, even for data files.

See also:[^],[^],[^].

In Visual Studio, to reference an assembly, you should use the tab ".NET" of the "Add Reference" window.

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

OK, thanks for the answer to my comment. I've just tested it and the problem is in the CompilerParameters passed to CompileAssemblyFromSource and you must have missed the reference to System.dll

String[] referenceAssemblies = { "System.dll" };
CompilerParameters cp = new CompilerParameters(referenceAssemblies);
cp.GenerateInMemory = true;
cp.GenerateExecutable = false;
cp.OutputAssembly = "My.Dynamic.dll";
CompilerResults cr = RuntimeCompiler.CompileAssemblyFromSource(cp, source);

Without that reference I get that same error "error CS0234: The type or namespace name 'ComponentModel' does not exist in the namespace 'System' (are you missing an assembly reference?)" when I execute the code.

Tarek Elqusi 14-Feb-13 20:06pm
Thanks Alan, I got the idea. I tried it and it worked successfully.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100