How do I create and use a .NET metadata-only 'Reference Assembly'?

后端 未结 4 951
栀梦
栀梦 2020-12-08 07:57

Since version 3.0, .NET installs a bunch of different \'reference assemblies\' under C:\\Program Files\\Reference Assemblies\\Microsoft...., to support different profiles (s

4条回答
  •  被撕碎了的回忆
    2020-12-08 08:28

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

    [assembly: ReferenceAssembly]
    

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

    Assembly.ReflectionOnlyLoad()

    or

    Assembly.ReflectionOnlyLoadFrom()


    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: 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.

    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...

提交回复
热议问题