Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote the following code to create a class dynamically

C#
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
C#
string s = @ "C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.dll"
and tried to load it using
C#
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
Comments
ZurdoDev 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.
—SA
Sergey Alexandrovich Kryukov 14-Feb-13 17:08pm    
I basically answered, please see.
Pretty difficult to explain thing for an inquirer on this level... :-)
—SA
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...
—SA
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=4.0.0.0, 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.

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.
 
Share this answer
 
v2
Comments
ZurdoDev 14-Feb-13 17:05pm    
OP is trying to build a compiler though.
fjdiewornncalwe 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....
ZurdoDev 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...
—SA
You are doing it wrong; and this is the fundamental thing. Without it, you cannot develop any .NET products. So, you are not developing. :-)

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:
http://en.wikipedia.org/wiki/Assembly_%28programming%29[^],
http://msdn.microsoft.com/en-us/library/hk5f40ct%28v=vs.90%29.aspx[^],
http://msdn.microsoft.com/en-us/library/s1sx4kfb.aspx[^],
http://en.wikipedia.org/wiki/Global_Assembly_Cache[^].

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:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^],
http://msdn.microsoft.com/en-us/library/x4cw969y.aspx[^],
http://msdn.microsoft.com/en-us/library/ky3942xh.aspx[^].

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

—SA
 
Share this answer
 
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

e.g.
C#
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.

Alan.
 
Share this answer
 
Comments
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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900