简体   繁体   中英

Creating DLL using Csharpcodeprovider and adding Resources to it

I have a DLL reader to extract resources(images) from dynamically created DLL. In order to test it, I have developed a DLL to specify the requirements needed for my DLL reader to extract images. The DLL worked as expected as I can manually include images into the resource folder. However, in the process of generating dynamic DLL I dont really understand the method to include resources into the solution before compiling it into DLL.

So the tools I have are:

  1. DLL reader (work)
  2. DLL with image resources (work)
  3. Dynamically generated DLL with image resource (not work)

(1) - (2) works well, (1) - (3) unable to get image

After debugging I found out that images were not added into the Dynamically added DLL.

Source code:

手动创建的 DLL

命名空间

Images included to DLL's resources, under namespace named DLLforWPF

When I execute my DLL reader to get the image and convert it into BitmapImage, it works.

Assembly currentAssembly = Assembly.LoadFrom("DLLforWPF.dll");
string[] resources = currentAssembly.GetManifestResourceNames();
//Please see the attached image for the results
Stream myStream = currentAssembly.GetManifestResourceStream("DLLforWPF.Resources.img1.png");
            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.StreamSource = myStream;
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.EndInit();
            bitmap.Freeze();

在此处输入图像描述

It displays the resources that I have added into the DLL.

Then I proceed to develop a solution to dynamically generate DLL with image resources. I have used stringbuilder to construct the code below:

//in stringbuilder
using System;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using System.Windows.Forms;

namespace ben
{
    public class Imgstored
    {
            [STAThread]
            public static void Main(string[] args)
            {
            }
    }
}
string executable = Application.ExecutablePath;
string baseDir = Path.GetDirectoryName(executable);
CodeDomProvider compiler = new CSharpCodeProvider(options.CompilerOptions);
CompilerParameters parameters = new CompilerParameters{ OutputAssembly = "ben.dll"};
parameters.WarningLevel = 4;
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;

if (compiler.Supports(GeneratorSupport.Resources))
{
parameters.EmbeddedResources.Add($"{Application.StartupPath}\\Temp\\audio.png");
}

I have tried changing the directory such as directory\audio1.png and it gave me exception of image not found. So I am sure that the resource pointed to the image directory.

When I execute DLL reader, 在此处输入图像描述

This is the result of my resources in dynamically created DLL. I am not able to convert it to BitmapImage as it is not an image resource. I suspect the process of adding image resource to CSharpCodeProvider is having some error, please tell me the problem of adding resource using CompilerParameters.EmbeddedResources.Add(image path). The result is totally different from the manually created DLL. There are very limited material regarding adding resources into dynamic DLL through CSharpCodeProvider. Thank you for your time.

In my functional code, i have just that:

        FileInfo fi = new FileInfo(@".\files_freepie\curves\CurveAssembly.dll");
        CSharpCodeProvider provider = new CSharpCodeProvider();
        CompilerParameters compilerparams = new CompilerParameters();
        compilerparams.OutputAssembly = @".\files_freepie\curves\CurveAssembly.dll";
        CompilerResults results = provider.CompileAssemblyFromFile(compilerparams, filename);

filename is the file to compile....

i dont use

CodeDomProvider compiler = new CSharpCodeProvider(options.CompilerOptions)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM