如何创建并使用.NET仅元数据“参考大会”? [英] How do I create and use a .NET metadata-only 'Reference Assembly'?

查看:266
本文介绍了如何创建并使用.NET仅元数据“参考大会”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于3.0版本,.NET安装了一堆不同的参考组件下的C:\Program Files\Reference Assemblies\Microsoft ....,以支持不同的配置文件(比如.NET 3.5客户端配置文件,Silverlight的配置文件)。每一种是只包含元数据的正确的.NET程序集 - 没有IL代码 - 而每个组件标有 ReferenceAssemblyAttribute 。元数据仅限于适用的配置文件下提供这些类型和成员 - 这就是智能感知是如何显示一组有限的类型和成员。参考组件在运行时不会使用。

Since version 3.0, .NET installs a bunch of different 'reference assemblies' under C:\Program Files\Reference Assemblies\Microsoft...., to support different profiles (say .NET 3.5 client profile, Silverlight profile). Each of these is a proper .NET assembly that contains only metadata - no IL code - and each assembly is marked with the ReferenceAssemblyAttribute. The metadata is restricted to those types and member available under the applicable profile - that's how intellisense shows a restricted set of types and members. The reference assemblies are not used at runtime.

从我的这个博客帖子

我想创建和使用这样我的库的引用程序集。

I'd like to create and use such a reference assembly for my library.


  1. 如何创建元数据只装配 - 是有一些编译器标志或反汇编后处理器?

  2. 是否有属性的哪些类型的出口到不同的配置文件

  3. 控制
  4. 如何参照大会决议在运行时 - 如果我有引用装配出现在我的应用程序目录,而不是真实的组装,而不是在GAC可言,将探测技术继续和我的AssemblyResolve事件火,这样我可以在运行时?

  5. 提供实际装配
  1. How do I create a metadata-only assembly - is there some compiler flag or ildasm post-processor?
  2. Are there attributes that control which types are exported to different 'profiles'?
  3. How does the reference assembly resolution at runtime - if I had the reference assembly present in my application directory instead of the 'real' assembly, and not in the GAC at all, would probing continue and my AssemblyResolve event fire so that I can supply the actual assembly at runtime?

任何意见或指针在那里我可以学到更多有关这将不胜感激。

Any ideas or pointers to where I could learn more about this would be greatly appreciated.

更新:的环顾了一下,我看到了.NET 3.0引用程序集似乎有一些代码,和的Reference大会属性只在.NET 4.0中添加的。所以行为可能已经改变了一下用新的运行时

Update: Looking around a bit, I see the .NET 3.0 'reference assemblies' do seem to have some code, and the Reference Assembly attribute was only added in .NET 4.0. So the behaviour might have changed a bit with the new runtime.

为什么的对于我的Excel-DNA(的http://exceldna.codeplex.com )附加库中,我创建了单文件.xll外接通过打包引用的程序集到.xll文件作为资源。填充组件包括用户的附加代码,以及将Excel-DNA的管理库(可能由用户的组件被引用)。

Why? For my Excel-DNA ( http://exceldna.codeplex.com ) add-in library, I create single-file .xll add-in by packing the referenced assemblies into the .xll file as resources. The packed assemblies include the user's add-in code, as well as the Excel-DNA managed library (which might be referenced by the user's assembly).

这听起来比较复杂,但工程的大部分时间以及奇妙 - 加载项是一个很小的文件,所以没有安装分布的问题。我碰到(并不令人意外),因为不同版本的问题 - 如果有旧版本的Excel的DNA管理库作为文件,运行时会加载,而不是装一(我没有机会与干扰加载)。

It sounds rather complicated, but works wonderfully well most of the time - the add-in is a single small file, so no installation of distribution issues. I run into (not unexpected) problems because of different versions - if there is an old version of the Excel-DNA managed library as a file, the runtime will load that instead of the packed one (I never get a chance to interfere with the loading).

我希望做一个参考组装我的Excel-DNA部分管理用户可以指向编译他们的加载项时。但是,如果他们错误有一个版本的组件在运行时,运行时应该无法加载它,并给我一个机会来加载从资源实际的汇编。

I hope to make a reference assembly for my Excel-DNA managed part that users can point to when compiling their add-ins. But if they mistakenly have a version of this assembly at runtime, the runtime should fail to load it, and give me a chance to load the real assembly from resources.

推荐答案

要创建一个引用集,你这行添加到您的的AssemblyInfo.cs 文件:

To create a reference assembly, you would add this line to your AssemblyInfo.cs file:

[assembly: ReferenceAssembly]

要加载其他人,您可以使用在运行时引用它们像往常一样从你的VisualStudio项目引用,或动态:

To load others, you can reference them as usual from your VisualStudio project references, or dynamically at runtime using:

Assembly.ReflectionOnlyLoad()

Assembly.ReflectionOnlyLoadFrom()

如果您已经添加使用汇编的VisualStudio,然后智能感知和建设项目将工作得很好,但是如果你试图执行一个元/参考参考你对一个应用程序,你会得到一个错误:

If you have added a reference to a metadata/reference assembly using VisualStudio, then intellisense and building your project will work just fine, however if you try to execute your application against one, you will get an error:

System.BadImageFormatException:无法加载引用程序集执行

System.BadImageFormatException: Cannot load a reference assembly for execution.

因此,期望的是,在运行时,你会在具有相同的元数据签名的实际装配替代。

So the expectation is that at runtime you would substitute in a real assembly that has the same metadata signature.

如果您与 Assembly.ReflectionOnlyLoad动态加载的程序集()那么你只能做到针对它的所有反射操作(读的类型,方法,属性,属性等,但无法动态调用任何人)。

If you have loaded an assembly dynamically with Assembly.ReflectionOnlyLoad() then you can only do all the reflection operations against it (read the types, methods, properties, attributes, etc, but can not dynamically invoke any of them).

我很好奇,你的用例是什么创建一个唯一的元数据组装。我从来没有在这之前做了,很想知道你是否已经找到了他们的一些有趣的使用...

I am curious as to what your use case is for creating a metadata-only assembly. I've never had to do that before, and would love to know if you have found some interesting use for them...

这篇关于如何创建并使用.NET仅元数据“参考大会”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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