如何使用C#在解决方案探索中以编程方式卸载和加载项目 [英] how to unload and load project programatically in solution explore using C#

查看:77
本文介绍了如何使用C#在解决方案探索中以编程方式卸载和加载项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.我正在使用CodeDOM创建动态类.但是现在该类未在解决方案资源管理器中显示,因此我必须手动添加该类.我不想手动执行此操作.然后,我通过编辑XML(例如
)来添加此类
< compile include ="*.cs"/>

添加类后,当我手动重新加载项目时可以显示它.因此,请给我一些解决方案,如何以编程方式在解决方案探索中加载和卸载项目.谢谢.

解决方案

原则上,这不是它的工作方式.当您从CodeDOM创建代码时,它将返回一些程序集.您应使用Reflection实例化此程序集的某些类以开始使用此类程序集.

请记住,您不能卸载装配体.唯一的方法是在内存中编译程序集(或加载程序集)是单独的AppDomain.可以卸载AppDomain,但是您需要IPC来处理其实例.要标识要实例化的类,您需要一些接口(请不要考虑按名称标识任何东西,它可以工作,但是极其不被支持.您还可以创建一个特殊的Assembly属性来标识要实例化的类型. .

有关所有更多详细信息,请参见我的其他答案:
创建使用可重载插件的WPF应用程序... [ ^ ]
使用CodeDom生成代码 [ StreamWriter sw =新的StreamWriter(codeFile);
CodeCompileUnit单位=新的CodeCompileUnit();
CodeTypeReference属性=新的CodeTypeReference(typeof(AssemblyVersionAttribute));
CodeAttributeDeclaration decl =新CodeAttributeDeclaration(attr,
new CodeAttributeArgument(new CodePrimitiveExpression("1.0.0.0"))));
unit.AssemblyCustomAttributes.Add(decl);
CSharpCodeProvider cscp =新的CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
ICodeCompiler codeCompiler = cscp.CreateCompiler();
cp.ReferencedAssemblies.Add("Business_layer.dll");
//生成可执行文件,而不是
//一个类库.
//cp.GenerateExecutable = false;
//设置要生成的程序集文件名.
cp.OutputAssembly = exeFile;
//将程序集另存为物理文件.
cp.GenerateInMemory = true;
ICodeGenerator codeGenerator = cscp.CreateGenerator(sw);
CodeGeneratorOptions cgo = new CodeGeneratorOptions();
//编译单元...
CodeSnippetCompileUnit cscu =新的CodeSnippetCompileUnit(正在使用系统;");
codeGenerator.GenerateCodeFromCompileUnit(cscu,sw,cgo);
cscp.GenerateCodeFromCompileUnit(unit,sw,new CodeGeneratorOptions());

//在什么是NameSpace中...
CodeNamespace cnsCodeDom =新的CodeNamespace("Business_Layer");

//在运行时声明类....
CodeTypeDeclaration ctd =新的CodeTypeDeclaration();
ctd.IsClass = true;
ctd.Name = NewFrame_Name_TxtBox.Text.ToString();
ctd.BaseTypes.Add("Business_layer");
ctd.TypeAttributes = TypeAttributes.Public;

cnsCodeDom.Types.Add(ctd);

//为类的函数生成代码.....
CodeMemberProperty函数=新的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();
}



那就是我使用CodeDom创建的类.现在,我只想在使用3层体系结构的解决方案Explore..im中添加此动态类.并且我想要嵌入SQL查询,该查询将从Window窗体中进一步调用. blockquote>

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.

解决方案

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


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..


这篇关于如何使用C#在解决方案探索中以编程方式卸载和加载项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆