-
Notifications
You must be signed in to change notification settings - Fork 90
Fix static member exports to not instantiate declaring type #594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
3d002e1
e660673
17f767f
63bfb57
c4ecf24
4648648
1c817b6
5c5690a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ namespace Microsoft.VisualStudio.Composition.Tests | |
| using Xunit; | ||
| using CompositionFailedException = Microsoft.VisualStudio.Composition.CompositionFailedException; | ||
| using MefV1 = System.ComponentModel.Composition; | ||
| using MefV2 = System.Composition; | ||
|
|
||
| [Trait("Static", "")] | ||
| public class StaticMemberExportsTests | ||
|
|
@@ -194,5 +195,104 @@ public class ImportManyWithMetadataPart | |
| [MefV1.ImportMany("Property")] | ||
| public List<Lazy<string, IDictionary<string, object>>> ImportingMember { get; set; } = null!; | ||
| } | ||
|
|
||
| // Test class to verify static members don't cause instantiation | ||
| public class ClassWithStaticMemberExports | ||
| { | ||
| private static bool constructorCalled = false; | ||
|
|
||
| public static bool ConstructorCalled | ||
| { | ||
| get { return constructorCalled; } | ||
| set { constructorCalled = value; } | ||
| } | ||
|
|
||
| public ClassWithStaticMemberExports() | ||
| { | ||
| constructorCalled = true; | ||
| } | ||
|
|
||
| [MefV1.Export("StaticField")] | ||
| [MefV2.Export("StaticField")] | ||
| public static string StaticField = "StaticFieldValue"; | ||
|
|
||
| [MefV1.Export("StaticProperty")] | ||
| [MefV2.Export("StaticProperty")] | ||
| public static string StaticProperty => "StaticPropertyValue"; | ||
|
|
||
| [MefV1.Export("StaticMethod")] | ||
| [MefV2.Export("StaticMethod")] | ||
AArnott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public static string StaticMethod() => "StaticMethodValue"; | ||
| } | ||
|
|
||
| // Test class with mixed static and instance exports | ||
| public class ClassWithMixedExports | ||
| { | ||
| private static bool constructorCalled = false; | ||
|
|
||
| public static bool ConstructorCalled | ||
| { | ||
| get { return constructorCalled; } | ||
| set { constructorCalled = value; } | ||
| } | ||
|
|
||
| public ClassWithMixedExports() | ||
| { | ||
| constructorCalled = true; | ||
| } | ||
|
|
||
| [MefV1.Export("StaticMixed")] | ||
| [MefV2.Export("StaticMixed")] | ||
AArnott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public static string StaticExport = "StaticValue"; | ||
|
|
||
| [MefV1.Export("InstanceMixed")] | ||
| [MefV2.Export("InstanceMixed")] | ||
| public string InstanceExport => "InstanceValue"; | ||
| } | ||
|
|
||
| [MefFact(CompositionEngines.V1Compat | CompositionEngines.V2Compat | CompositionEngines.V3EmulatingV1 | CompositionEngines.V3EmulatingV2, typeof(ClassWithStaticMemberExports))] | ||
| public void StaticFieldExportDoesNotInstantiateClass(IContainer container) | ||
| { | ||
| ClassWithStaticMemberExports.ConstructorCalled = false; | ||
| var value = container.GetExportedValue<string>("StaticField"); | ||
| Assert.Equal("StaticFieldValue", value); | ||
| Assert.False(ClassWithStaticMemberExports.ConstructorCalled, "Constructor should not be called for static field export"); | ||
| } | ||
|
|
||
| [MefFact(CompositionEngines.V1Compat | CompositionEngines.V2Compat | CompositionEngines.V3EmulatingV1 | CompositionEngines.V3EmulatingV2, typeof(ClassWithStaticMemberExports))] | ||
| public void StaticPropertyExportDoesNotInstantiateClass(IContainer container) | ||
| { | ||
| ClassWithStaticMemberExports.ConstructorCalled = false; | ||
| var value = container.GetExportedValue<string>("StaticProperty"); | ||
| Assert.Equal("StaticPropertyValue", value); | ||
| Assert.False(ClassWithStaticMemberExports.ConstructorCalled, "Constructor should not be called for static property export"); | ||
| } | ||
|
|
||
| [MefFact(CompositionEngines.V1Compat | CompositionEngines.V2Compat | CompositionEngines.V3EmulatingV1 | CompositionEngines.V3EmulatingV2, typeof(ClassWithStaticMemberExports))] | ||
|
||
| public void StaticMethodExportDoesNotInstantiateClass(IContainer container) | ||
| { | ||
| ClassWithStaticMemberExports.ConstructorCalled = false; | ||
| var value = container.GetExportedValue<Func<string>>("StaticMethod"); | ||
| Assert.Equal("StaticMethodValue", value()); | ||
| Assert.False(ClassWithStaticMemberExports.ConstructorCalled, "Constructor should not be called for static method export"); | ||
| } | ||
|
|
||
| [MefFact(CompositionEngines.V1Compat | CompositionEngines.V2Compat | CompositionEngines.V3EmulatingV1 | CompositionEngines.V3EmulatingV2, typeof(ClassWithMixedExports))] | ||
| public void StaticExportInMixedClassDoesNotInstantiateClass(IContainer container) | ||
| { | ||
| ClassWithMixedExports.ConstructorCalled = false; | ||
| var value = container.GetExportedValue<string>("StaticMixed"); | ||
| Assert.Equal("StaticValue", value); | ||
| Assert.False(ClassWithMixedExports.ConstructorCalled, "Constructor should not be called when accessing only static export"); | ||
| } | ||
|
|
||
| [MefFact(CompositionEngines.V1Compat | CompositionEngines.V2Compat | CompositionEngines.V3EmulatingV1 | CompositionEngines.V3EmulatingV2, typeof(ClassWithMixedExports))] | ||
| public void InstanceExportInMixedClassDoesInstantiateClass(IContainer container) | ||
| { | ||
| ClassWithMixedExports.ConstructorCalled = false; | ||
| var value = container.GetExportedValue<string>("InstanceMixed"); | ||
| Assert.Equal("InstanceValue", value); | ||
| Assert.True(ClassWithMixedExports.ConstructorCalled, "Constructor should be called when accessing instance export"); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.