You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docfx/analyzers/VSMEF008.md
+27-4Lines changed: 27 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,11 @@ When using `[Import(typeof(T))]` or `[ImportMany(typeof(T))]` with an explicit c
4
4
5
5
## Cause
6
6
7
-
An import specifies a `ContractType` that is incompatible with the member type receiving the import. This will cause a composition failure at runtime.
7
+
An import specifies a `ContractType` that is incompatible with the member type receiving the import. This may cause a composition failure at runtime.
8
8
9
9
## Rule description
10
10
11
-
MEF allows you to specify an explicit contract type using `[Import(typeof(T))]` or `[ImportMany(typeof(T))]`. The exports matching this contract must be assignable to the member where the import is declared. If the types are incompatible, composition will fail at runtime.
11
+
MEF allows you to specify an explicit contract type using `[Import(typeof(T))]` or `[ImportMany(typeof(T))]`. The exports matching this contract must be assignable to the member where the import is declared. If the types are incompatible, composition may fail at runtime.
12
12
13
13
This analyzer detects the following incompatibilities:
14
14
@@ -152,9 +152,32 @@ public IDatabase Database { get; set; } // Change contract to match member type
152
152
publicILoggerLogger { get; set; } // Let MEF infer the contract type
153
153
```
154
154
155
-
##When to suppress errors
155
+
### Option 4: Add an allow-list entry (for SDK scenarios)
156
156
157
-
This error should generally not be suppressed as it indicates a bug that will cause runtime failures. If you're doing something unusual with MEF that the analyzer doesn't understand, ensure you have tests that verify the composition works correctly.
157
+
Some SDKs use a type identity as a contract name even when that type is not assignable to the import type. For example, the Visual Studio SDK uses `SVsFullAccessServiceBroker` as a contract name for imports of type `IServiceBroker`.
158
+
159
+
If your project relies on such a convention, you can suppress this warning for specific pairs by adding an `AdditionalFiles` entry named `vs-mef.ContractNamesAssignability.txt` to your project:
Each line declares that `ContractType` is a known-good contract name for an import of `MemberType`, even though the types are not statically assignable.
177
+
178
+
## When to suppress warnings
179
+
180
+
This warning indicates a potential bug in most cases. However, if you're using a contract name that is a type identity not assignable to the import type (a common SDK pattern), consider using the allow-list approach described in Option 4 instead of suppressing the diagnostic globally.
Copy file name to clipboardExpand all lines: test/Microsoft.VisualStudio.Composition.Analyzers.Tests/VSMEF008ImportContractTypeMismatchAnalyzerTests.cs
+87Lines changed: 87 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
0 commit comments