Search code examples
c#dll.net-assembly

Create dll file in runtime doesn't work in c#


I am trying to create a DLL file in runtime ,as a matter of fact i need to save an encoded data to DLL .My code is like this :

 class DllFile
    {
        public static void CreateDllFile(string source)
        {
            var provider = new CSharpCodeProvider();
            var options = new CompilerParameters
            {
                OutputAssembly = "test.dll"
            };

        var results = provider.CompileAssemblyFromSource(options, new[] { source });
        }
    }

I expect from this code to create a dll file but it doesn't create

The error is :The pointer for this method was null

Best regards.Any ideas will be appreciated.


Solution

  • Compilation errors are reported via the returned value:

    var results = provider.CompileAssemblyFromSource(options, new[] { source });
    

    Now check results, and in particular results.Errors.

    You can also check results.NativeCompilerReturnValue - that should be 0 for success, and non-zero for failure.