Is there a way to build a new type during Runtime?

前端 未结 4 1108
[愿得一人]
[愿得一人] 2020-12-28 15:47

I am going to ask a question that might sound weird.

Is there a way to build a new class during Runtime? Or at least, add a new property to an existing class.

<
4条回答
  •  甜味超标
    2020-12-28 16:16

    This is not a weird question - in some cases it might be very useful. For instance I use this technique for performance tests sometimes:

    public static Type[] DynamicTypes;
    
    public void CreateObjects()
    {
      var codeNamespace = new CodeNamespace( "DynamicClasses" );
      codeNamespace.Imports.Add( new CodeNamespaceImport( "System" ) );
      codeNamespace.Imports.Add( new CodeNamespaceImport( "System.ComponentModel" ) );
    
      for( var i = 0; i < 2000; i++ )
      {
        var classToCreate = new CodeTypeDeclaration( "DynamicClass_" + i )
        {
          TypeAttributes = TypeAttributes.Public
        };
        var codeConstructor1 = new CodeConstructor
        {
          Attributes = MemberAttributes.Public
        };
        classToCreate.Members.Add( codeConstructor1 );
    
        codeNamespace.Types.Add( classToCreate );
      }
    
      var codeCompileUnit = new CodeCompileUnit();
      codeCompileUnit.Namespaces.Add( codeNamespace );
    
      var compilerParameters = new CompilerParameters
      {
        GenerateInMemory = true,
        IncludeDebugInformation = true,
        TreatWarningsAsErrors = true,
        WarningLevel = 4
      };
      compilerParameters.ReferencedAssemblies.Add( "System.dll" );
    
      var compilerResults = new CSharpCodeProvider().CompileAssemblyFromDom( compilerParameters, codeCompileUnit );
    
      if( compilerResults == null )
      {
        throw new InvalidOperationException( "ClassCompiler did not return results." );
      }
      if( compilerResults.Errors.HasErrors )
      {
        var errors = string.Empty;
        foreach( CompilerError compilerError in compilerResults.Errors )
        {
          errors += compilerError.ErrorText + "\n";
        }
        Debug.Fail( errors );
        throw new InvalidOperationException( "Errors while compiling the dynamic classes:\n" + errors );
      }
    
      var dynamicAssembly = compilerResults.CompiledAssembly;
      DynamicTypes = dynamicAssembly.GetExportedTypes();
    }
    

提交回复
热议问题