Json.Net 可以嵌入到可执行文件中吗?
Can Json.Net be embedded into the executable?
我将 Netwonsoft.Json
库的 'Embed Interop Types' 属性 设置为 true
并且 returns 出现错误:
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
嵌入到可执行文件中?
你没有说你使用的是哪种语言,但这里是你如何为 C# 做的
首先,关闭"Embed Interop Types"
然后,到主要的可执行项目,卸载并编辑.csproj文件,在下面一行:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
将此 XML 添加到项目文件中,保存并重新加载它。
<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):
[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;
}
最后,确保将主应用程序的目标方法更新为刚刚添加的项目的主要方法
来源: http://www.paulrohde.com/merging-a-wpf-application-into-a-single-exe/
我将 Netwonsoft.Json
库的 'Embed Interop Types' 属性 设置为 true
并且 returns 出现错误:
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
嵌入到可执行文件中?
你没有说你使用的是哪种语言,但这里是你如何为 C# 做的
首先,关闭"Embed Interop Types"
然后,到主要的可执行项目,卸载并编辑.csproj文件,在下面一行:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
将此 XML 添加到项目文件中,保存并重新加载它。
<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):
[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;
}
最后,确保将主应用程序的目标方法更新为刚刚添加的项目的主要方法
来源: http://www.paulrohde.com/merging-a-wpf-application-into-a-single-exe/