我有一个C#应用程序,我想通过使用CodeDom重新创建。这个应用程序有四个类。如果我要进入这个应用程序目录,我会找到项目文件(App.csproj),如果我要启动这个项目文件,所有四个类将加载在一起。此外,如果我要构建这个应用程序,所有四个类将构建在一起。
我的问题:我可以通过使用CodeDom来创建此功能吗?
我已经成功地使用CodeDom创建了四个类之一,但是如何创建下三个类(并将它们链接到我已经创建的第一个类)
我知道这可能听起来很混乱,但如果有必要,我会解释更多。
解决方案相同的命名空间,您可以将它们全部添加到一个 CodeNamespace 对象并从中生成代码。
如果在不同的命名空间中,您可以通过将其他类的命名空间引用添加到您正在工作的命名空间对象中,将其他类的命名空间添加到您的第一个类: -
//将另一个类的命名空间添加到当前命名空间中 defaultNameSpace.Imports.Add(new CodeNamespaceImport( Namespace.Namespace));
其中defaultNameSpace是 CodeNamespace 的一种类型。你构建的第一个类被添加到这个CodeNamespace对象,如下所示,然后生成代码: -
defaultNameSpace.Types.Add(mainClass) ;
这有助于。
I have a C# application that I am trying to re-create through the use of CodeDom. This application has four classes inside of it. If I were to go into this applications directory, I would find the project file (App.csproj), and if I were to start this project file, all four classes would load together. Furthermore, if I were to build this application, all four classes would build together.
My Question: How on earth can I create this functionality through the use of CodeDom?
I have sucessfully created one of the four classes using CodeDom, but how can I go about creating the next three classes (and linking them) to the first class that I already created?
I know this may sound confusing but I will explain more if necessary.
解决方案If the classes are in the same namespace you can add them all to one CodeNamespace object and generate the code from that.
If there in different namespaces you can add the namespace of the other Classes to your first class by adding the namespaces reference of the other class's to the namespace object you are working in:-
// Add the Namespace of the other class to the current namespace onject defaultNameSpace.Imports.Add(new CodeNamespaceImport("Project.Namespace.Namespace"));
Where defaultNameSpace is a type of CodeNamespace. The first Class you have built is added to this CodeNamespace object as below and then the code is generated from that :-
defaultNameSpace.Types.Add(mainClass);
mainClass being a type of CodeTypeDeclaration.
Hope this helps.
更多推荐
CodeDom
发布评论