开发者

Create Dll dynamic from c#

开发者 https://www.devze.com 2023-02-12 04:51 出处:网络
Hey, I use the CSharpCodeProvider class to compile c# code into a dll. That works fine, but only with sourcecode. I would compile into my dll Resources.

Hey, I use the CSharpCodeProvider class to compile c# code into a dll.

That works fine, but only with sourcecode. I would compile into my dll Resources.

I have found the class

CodeCompileUnit codeCompileUnit =
    StronglyTypedResourceBuilder.Create(
       dicResources, "Resources", 
       "Customer.Premium.XmlAppResources.Properties", cSharpCodeProvider,
       true, out errors
    );

this class creates my a perfect c# class that i can compile and i get a wonderfull dll =) BUT :P ...

if i use this dll and 开发者_运维百科i work with the CultureInfo.CurrentCulture i get an error like this :

For the specified culture or the neutral culture resources could not be found. Make sure that Customer.Premium.XmlAppResources.Properties.Resources.resources embedded at compile correctly in the assembly Customer.Premium.XmlAppResources was, or that the required satellite assemblies can be loaded and completely unsigned.

I have no Customer.Premium.XmlAppResources.Properties.Resources.resources in my dll.. and i have no idee where i get this from ...


If I understand your question correctly: You can do this from the command line using resgen.exe. It will generate a resource file you can use from .Net applications.


You're making this way harder than it needs to be. Just choose a Class Library Project type when creating your project and the output will be a dll, resources and all.


The issue you have is that the StronglyTypedResourceBuilder creates from your resources a C# class which has a property ResourceManager where this one has the wrong namespace or you generate a ResourceManager by hand and assume a different resource name. From where did you get the ResourceManager that did cause the problem? You can work around this by using an overload of the ResourceBuilder where you can specify the Resource Base Name, Resource Name and the namespace of the generated class. The naming scheme is

  • Resource Base Name: Customer.Premium.XmlAppResources.Properties
  • Resource Name: Resources
  • Resource Extension: .ressources

This 3 things define the resource name which is stored as resource stream in your assembly. In this case Customer.Premium.XmlAppResources.Properties.Resources.ressources

Please check with e.g. Reflector if under which name the resources are created in your assembly and alter your namespace accordingly to make either the creator of the ResourceManager happy or change the creation of the ResourceManager to successfully load the resources.

Yours, Alois Kraus


Yes Thanks! This is exactly what I need:

  IResourceWriter writer = new ResourceWriter("myResources.resources");

  // Adds resources to the resource writer.
  writer.AddResource("String 1", "First String");

  writer.AddResource("String 2", "Second String");

  writer.AddResource("String 3", "Third String");

  // Writes the resources to the file or stream, and closes it.
  writer.Close();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号