为了生成这段代码,我需要编写什么编码?

时间:2021-09-16 08:00:07

I need to do this.

我需要这样做。

public class MyClass{

    private static IDictionary<String, Type> databaseAccessClasses
                              = new Dictionary<String, Type>();

    private static IDictionary<String, Type> DatabaseAccessClasses
    {
        get { return DataAccessFactory.databaseAccessClasses; }
        set { DataAccessFactory.databaseAccessClasses = value; }
    }

}

2 个解决方案

#1


Use the following code:

使用以下代码:

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace nameSpace = new CodeNamespace("xy");

compileUnit.Namespaces.Add(nameSpace);

CodeTypeDeclaration myClass = new CodeTypeDeclaration("MyClass");
nameSpace.Types.Add(myClass);
myClass.TypeAttributes = System.Reflection.TypeAttributes.Class | System.Reflection.TypeAttributes.Public;

CodeTypeReference dictionaryType = new CodeTypeReference("IDictionary", new CodeTypeReference(typeof(string)), new CodeTypeReference(typeof(Type)));

CodeMemberField myClass_databaseAccessClasses = new CodeMemberField(dictionaryType, "databaseAccessClasses");
myClass_databaseAccessClasses.Attributes = MemberAttributes.Static;
myClass.Members.Add(myClass_databaseAccessClasses);

CodeMemberProperty myClass_DatabaseAccessClasses = new CodeMemberProperty();
myClass_DatabaseAccessClasses.Name = "DatabaseAccessClasses";
myClass_DatabaseAccessClasses.Type = dictionaryType;
myClass_DatabaseAccessClasses.Attributes = MemberAttributes.Static;
myClass_DatabaseAccessClasses.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(new CodeTypeReference("DataAccessFactory")), "databaseAccessClasses")));
myClass_DatabaseAccessClasses.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(new CodeTypeReference("DataAccessFactory")), "databaseAccessClasses"), new CodeArgumentReferenceExpression("value")));
myClass.Members.Add(myClass_DatabaseAccessClasses);

StringWriter writer = new StringWriter();
codeProvider.GenerateCodeFromCompileUnit(compileUnit, writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
// writer.ToString() contains the C# code

Rüdiger

#2


Rudiger's answer is close, but needs a few minor modifications to actually generate the requested code:

Rudiger的答案很接近,但需要进行一些小的修改才能真正生成所请求的代码:

        CSharpCodeProvider codeProvider = new CSharpCodeProvider();
        CodeCompileUnit compileUnit = new CodeCompileUnit();
        CodeNamespace nameSpace = new CodeNamespace( "xy" );

        compileUnit.Namespaces.Add( nameSpace );

        CodeTypeDeclaration myClass = new CodeTypeDeclaration( "MyClass" );
        nameSpace.Types.Add( myClass );
        myClass.TypeAttributes = System.Reflection.TypeAttributes.Class | System.Reflection.TypeAttributes.Public;

        CodeTypeReference dictionaryType = new CodeTypeReference( "IDictionary", new CodeTypeReference( typeof( string ) ), new CodeTypeReference( typeof( Type ) ) );

        CodeMemberField myClass_databaseAccessClasses = new CodeMemberField( dictionaryType, "databaseAccessClasses" );
        myClass_databaseAccessClasses.Attributes = MemberAttributes.Static | MemberAttributes.Private;
        myClass_databaseAccessClasses.InitExpression = new CodeObjectCreateExpression(dictionaryType);
        myClass.Members.Add( myClass_databaseAccessClasses );

        CodeMemberProperty myClass_DatabaseAccessClasses = new CodeMemberProperty();
        myClass_DatabaseAccessClasses.Name = "DatabaseAccessClasses";
        myClass_DatabaseAccessClasses.Type = dictionaryType;
        myClass_DatabaseAccessClasses.Attributes = MemberAttributes.Static | MemberAttributes.Private;
        myClass_DatabaseAccessClasses.GetStatements.Add( new CodeMethodReturnStatement( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression( new CodeTypeReference( "DataAccessFactory" ) ), "databaseAccessClasses" ) ) );
        myClass_DatabaseAccessClasses.SetStatements.Add( new CodeAssignStatement( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression( new CodeTypeReference( "DataAccessFactory" ) ), "databaseAccessClasses" ), new CodeArgumentReferenceExpression( "value" ) ) );
        myClass.Members.Add( myClass_DatabaseAccessClasses );

        StringWriter writer = new StringWriter();
        codeProvider.GenerateCodeFromCompileUnit( compileUnit, writer, new System.CodeDom.Compiler.CodeGeneratorOptions() );

#1


Use the following code:

使用以下代码:

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace nameSpace = new CodeNamespace("xy");

compileUnit.Namespaces.Add(nameSpace);

CodeTypeDeclaration myClass = new CodeTypeDeclaration("MyClass");
nameSpace.Types.Add(myClass);
myClass.TypeAttributes = System.Reflection.TypeAttributes.Class | System.Reflection.TypeAttributes.Public;

CodeTypeReference dictionaryType = new CodeTypeReference("IDictionary", new CodeTypeReference(typeof(string)), new CodeTypeReference(typeof(Type)));

CodeMemberField myClass_databaseAccessClasses = new CodeMemberField(dictionaryType, "databaseAccessClasses");
myClass_databaseAccessClasses.Attributes = MemberAttributes.Static;
myClass.Members.Add(myClass_databaseAccessClasses);

CodeMemberProperty myClass_DatabaseAccessClasses = new CodeMemberProperty();
myClass_DatabaseAccessClasses.Name = "DatabaseAccessClasses";
myClass_DatabaseAccessClasses.Type = dictionaryType;
myClass_DatabaseAccessClasses.Attributes = MemberAttributes.Static;
myClass_DatabaseAccessClasses.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(new CodeTypeReference("DataAccessFactory")), "databaseAccessClasses")));
myClass_DatabaseAccessClasses.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(new CodeTypeReference("DataAccessFactory")), "databaseAccessClasses"), new CodeArgumentReferenceExpression("value")));
myClass.Members.Add(myClass_DatabaseAccessClasses);

StringWriter writer = new StringWriter();
codeProvider.GenerateCodeFromCompileUnit(compileUnit, writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
// writer.ToString() contains the C# code

Rüdiger

#2


Rudiger's answer is close, but needs a few minor modifications to actually generate the requested code:

Rudiger的答案很接近,但需要进行一些小的修改才能真正生成所请求的代码:

        CSharpCodeProvider codeProvider = new CSharpCodeProvider();
        CodeCompileUnit compileUnit = new CodeCompileUnit();
        CodeNamespace nameSpace = new CodeNamespace( "xy" );

        compileUnit.Namespaces.Add( nameSpace );

        CodeTypeDeclaration myClass = new CodeTypeDeclaration( "MyClass" );
        nameSpace.Types.Add( myClass );
        myClass.TypeAttributes = System.Reflection.TypeAttributes.Class | System.Reflection.TypeAttributes.Public;

        CodeTypeReference dictionaryType = new CodeTypeReference( "IDictionary", new CodeTypeReference( typeof( string ) ), new CodeTypeReference( typeof( Type ) ) );

        CodeMemberField myClass_databaseAccessClasses = new CodeMemberField( dictionaryType, "databaseAccessClasses" );
        myClass_databaseAccessClasses.Attributes = MemberAttributes.Static | MemberAttributes.Private;
        myClass_databaseAccessClasses.InitExpression = new CodeObjectCreateExpression(dictionaryType);
        myClass.Members.Add( myClass_databaseAccessClasses );

        CodeMemberProperty myClass_DatabaseAccessClasses = new CodeMemberProperty();
        myClass_DatabaseAccessClasses.Name = "DatabaseAccessClasses";
        myClass_DatabaseAccessClasses.Type = dictionaryType;
        myClass_DatabaseAccessClasses.Attributes = MemberAttributes.Static | MemberAttributes.Private;
        myClass_DatabaseAccessClasses.GetStatements.Add( new CodeMethodReturnStatement( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression( new CodeTypeReference( "DataAccessFactory" ) ), "databaseAccessClasses" ) ) );
        myClass_DatabaseAccessClasses.SetStatements.Add( new CodeAssignStatement( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression( new CodeTypeReference( "DataAccessFactory" ) ), "databaseAccessClasses" ), new CodeArgumentReferenceExpression( "value" ) ) );
        myClass.Members.Add( myClass_DatabaseAccessClasses );

        StringWriter writer = new StringWriter();
        codeProvider.GenerateCodeFromCompileUnit( compileUnit, writer, new System.CodeDom.Compiler.CodeGeneratorOptions() );