从c#创建动态Dll

时间:2011-02-21 18:08:30

标签: c# dll

嘿 我使用CSharpCodeProvider类将c#代码编译成一个dll。

工作正常,但仅限于源代码。我会编译成我的dll资源。

我找到了班级

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

这个类创建了一个完美的c#类,我可以编译,我得到一个很棒的dll =)但是:P ...

如果我使用这个DLL并且我使用CultureInfo.CurrentCulture我会收到如下错误:

  

对于指定的文化或中立文化资源无法找到。确保在程序集Customer.Premium.XmlAppResources中正确编译Customer.Premium.XmlAppResources.Properties.Resources.resources,或者可以加载所需的附属程序集并完全无符号。

我的dll中没有Customer.Premium.XmlAppResources.Properties.Resources.resources ..而且我没有想到我从哪里得到这个......

4 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题:您可以使用resgen.exe从命令行执行此操作。它将生成一个可以从.Net应用程序中使用的资源文件。

答案 1 :(得分:0)

你这样做比你需要的更难。只需在创建项目时选择类库项目类型,输出将是dll,resources和all。

答案 2 :(得分:0)

你遇到的问题是StronglyTypedResourceBuilder从你的资源创建一个C#类,它有一个属性ResourceManager,其中这个属性具有错误的命名空间,或者你手动生成一个ResourceManager并假设一个不同的资源名称。 从哪里获得导致问题的ResourceManager? 您可以使用ResourceBuilder的overload来解决此问题,您可以在其中指定资源库名称,资源名称和生成的类的命名空间。 命名方案是

  • 资源库名称:Customer.Premium.XmlAppResources.Properties
  • 资源名称:资源
  • 资源扩展:.ressources

这3件事定义了资源名称,它在程序集中作为资源流存储。在这种情况下,Customer.Premium.XmlAppResources.Properties.Resources.ressources

请查看,例如Reflector,如果在程序集中创建资源的名称,并相应地更改命名空间以使ResourceManager的创建者满意或更改ResourceManager的创建以成功加载资源。

此致,   Alois Kraus

答案 3 :(得分:0)

是的,谢谢!这正是我所需要的:

  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();