Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi. I'm creating a dynamic class using CodeDOM. But now this class is not shown in Solution Explorer, therefore i have to manually add that class. I dont want to manully do this. Then I add this class by editing XML such as

<compile include="*.cs" />

After the class has added it can be shown when I reload project manually. So please give me some solution how to load and unload project in solution explore programatically. Thanks.
Posted
Updated 23-Mar-11 22:15pm
v3

This is not how it can work in principle. When you create a code from CodeDOM, it returns you some assembly. You should use Reflection to instantiate some class(es) of this assembly to start working with such assembly.

Remember, you cannot unload assembly. The only way is to compile assembly in memory (or load it) is a separate AppDomain. AppDomain can be unloaded, but you need IPC to work with its instance. To identify the class to instantiate, you need some interface (please, don't even think about identification of anything by name, it will work, but extremely unsupportable. You can also create a special assembly attribute to identify the type to be instantiated.

For all further detail, see my other Answers:
Create WPF Application that uses Reloadable Plugins...[^]
code generating using CodeDom[^].

You will find a lot of useful detail and skeletons of some systems.

—SA
 
Share this answer
 
v2
Comments
dooa 25-Mar-11 3:19am    
actually i m new in C#..soo can you elaborate it...or can you paste any example..
Sergey Alexandrovich Kryukov 25-Mar-11 12:30pm    
Did you read all the material I referenced?
A very short answer would be: If you're novice: forger all your ideas and learn well the development cycle and application cycle -- it looks like your don't understand what's going on.
--SA
private void Btn_Save_Click(object sender, EventArgs e)
{
// Creating a new file stream ....
Stream codeFile = File.Open("C:\\Documents and Settings\\ummaraahmed\\Desktop\\Frame_Based_ES\\Business_Layer\\" + NewFrame_Name_TxtBox.Text.ToString() + ".cs", FileMode.Create);
StreamWriter sw = new StreamWriter(codeFile);
CodeCompileUnit unit = new CodeCompileUnit();
CodeTypeReference attr = new CodeTypeReference(typeof(AssemblyVersionAttribute));
CodeAttributeDeclaration decl = new CodeAttributeDeclaration(attr,
new CodeAttributeArgument(new CodePrimitiveExpression("1.0.0.0")));
unit.AssemblyCustomAttributes.Add(decl);
CSharpCodeProvider cscp = new CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
ICodeCompiler codeCompiler = cscp.CreateCompiler();
cp.ReferencedAssemblies.Add("Business_layer.dll");
// Generate an executable instead of
// a class library.
// cp.GenerateExecutable = false;
// Set the assembly file name to generate.
cp.OutputAssembly = exeFile;
// Save the assembly as a physical file.
cp.GenerateInMemory = true;
ICodeGenerator codeGenerator = cscp.CreateGenerator(sw);
CodeGeneratorOptions cgo = new CodeGeneratorOptions();
// Compile Unit ...
CodeSnippetCompileUnit cscu = new CodeSnippetCompileUnit("using System;");
codeGenerator.GenerateCodeFromCompileUnit(cscu, sw, cgo);
cscp.GenerateCodeFromCompileUnit(unit, sw, new CodeGeneratorOptions());

// Having in what is NameSpace...
CodeNamespace cnsCodeDom = new CodeNamespace("Business_Layer");

// Declaration of Class at runtime ....
CodeTypeDeclaration ctd = new CodeTypeDeclaration();
ctd.IsClass = true;
ctd.Name = NewFrame_Name_TxtBox.Text.ToString();
ctd.BaseTypes.Add("Business_layer");
ctd.TypeAttributes = TypeAttributes.Public;

cnsCodeDom.Types.Add(ctd);

// Code generating for Function of the Class .....
CodeMemberProperty function = new CodeMemberProperty();
function.Name = "Check_Format_Length()";
function.Type = new CodeTypeReference("");
function.Attributes = MemberAttributes.Override | MemberAttributes.Public;

ctd.Members.Add(function);

codeGenerator.GenerateCodeFromNamespace(cnsCodeDom, sw, cgo);
sw.Close();
codeFile.Close();
}



that is the class i m creating using CodeDom..now i just want to add this dynmic class in Solution Explore..i m using 3 layerd architecture..and i want to embed SQL queries which will be further called from Window forms..
 
Share this answer
 

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



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