Json.Net可以嵌入到可执行文件中吗? [英] Can Json.Net be embedded into the executable?

查看:69
本文介绍了Json.Net可以嵌入到可执行文件中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Netwonsoft.Json库的'Embed Interop Types'属性设置为true,它返回错误:

I set the 'Embed Interop Types' property of the Netwonsoft.Json library to true and it returns an error:

Cannot embed interop types from assembly
'c:\path\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll'
because it is missing either the 'ImportedFromTypeLibAttribute' attribute or
the 'PrimaryInteropAssemblyAttribute' attribute
c:\path\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll

看起来在Newtonsoft.Json库中寻找丢失的引用,但我不确定. Json.Net是否可以嵌入到可执行文件中?

It looks like looking for missing references within the Newtonsoft.Json library, but I am not entirely certain. Is it possible for Json.Net to be embeded into the executable?

推荐答案

您没有说您使用的是哪种语言,但这是您在C#中的使用方式

You didn't say which language you were using but here is how you'd do it for C#

首先,关闭嵌入式互操作类型"

First, turn off "Embed Interop Types"

然后,在主可执行项目中,卸载并编辑.csproj文件,并在以下行下方:

Then, to the main executable project, unload and edit the .csproj file, and below the following line:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

将此XML添加到项目文件中,保存并重新加载.

Add this XML to the project file, save, and load it back up.

 <Target Name="AfterResolveReferences">
  <ItemGroup>
    <EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll'">
      <LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
    </EmbeddedResource>
  </ItemGroup>
</Target>

然后,您将在主项目中添加一个新的代码文件,并向其添加以下代码(已修改以适合您的应用程序的命名/结构化),在WPF应用程序中,放置该文件的一个好地方就是App. xaml.cs):

You’ll then add a new code file to the main project and add the following code to it (modified to fit how your application is named / structured, in a WPF application, a good place to put it would be App.xaml.cs):

[STAThread]
public static void Main()
{
    AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;

    App.Main(); // Run WPF startup code.
}

private static Assembly OnResolveAssembly(object sender, ResolveEventArgs e)
{
    var thisAssembly = Assembly.GetExecutingAssembly();

    // Get the Name of the AssemblyFile
    var assemblyName = new AssemblyName(e.Name);
    var dllName = assemblyName.Name + ".dll";

    // Load from Embedded Resources - This function is not called if the Assembly is already
    // in the same folder as the app.
    var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(dllName));
    if (resources.Any())
    {

        // 99% of cases will only have one matching item, but if you don't,
        // you will have to change the logic to handle those cases.
        var resourceName = resources.First();
        using (var stream = thisAssembly.GetManifestResourceStream(resourceName))
        {
            if (stream == null) return null;
            var block = new byte[stream.Length];

            // Safely try to load the assembly.
            try
            {
                stream.Read(block, 0, block.Length);
                return Assembly.Load(block);
            }
            catch (IOException)
            {
                return null;
            }
            catch(BadImageFormatException)
            {
                return null;
            }
        }
    }

    // in the case the resource doesn't exist, return null.
    return null;
}

最后,确保将主应用程序的目标方法更新为刚添加的项目的主方法

Finally, make sure you update the target method for your main application to be the main method for the project you just added

来源: http://www.paulrohde.com/merging-a-wpf-application-into-a-single-exe/

这篇关于Json.Net可以嵌入到可执行文件中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆