Skip to content

Commit ae20c2e

Browse files
authored
Remove #ifs in System.ComponentModel.TypeConverter (#62364)
Make 2 scenarios work like they did on .NET Framework: 1. Skipping GuidAttribute and InterfaceTypeAttribute on interfaces. 2. GetExtenderProviders checks `component.Site.Container` for extenders * Remove #ifs in System.ComponentModel.TypeConverter * use reflection instead of making internals visible to test assembly * make pass broken trimming test * drop reflection
1 parent 2ecc0e2 commit ae20c2e

File tree

6 files changed

+181
-11
lines changed

6 files changed

+181
-11
lines changed

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/Design/StandardCommands.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,6 @@ private static class VSStandardCommands
662662
internal const int cmdidPanePrevTab = 287;
663663
internal const int cmdidPaneCloseToolWindow = 288;
664664
internal const int cmdidPaneActivateDocWindow = 289;
665-
#if DCR27419
666-
internal const int cmdidDockingViewMDI = 290;
667-
#endif
668665
internal const int cmdidDockingViewFloater = 291;
669666
internal const int cmdidAutoHideWindow = 292;
670667
internal const int cmdidMoveToDropdownBar = 293;

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectTypeDescriptionProvider.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ private static Type[] InitializeSkipInterfaceAttributeList()
7979
{
8080
return new Type[]
8181
{
82-
#if FEATURE_SKIP_INTERFACE
8382
typeof(System.Runtime.InteropServices.GuidAttribute),
84-
typeof(System.Runtime.InteropServices.InterfaceTypeAttribute)
85-
#endif
83+
typeof(System.Runtime.InteropServices.InterfaceTypeAttribute),
8684
typeof(System.Runtime.InteropServices.ComVisibleAttribute),
8785
};
8886
}
@@ -649,16 +647,14 @@ protected internal override IExtenderProvider[] GetExtenderProviders(object inst
649647
{
650648
return GetExtenders(extenderList.GetExtenderProviders(), instance, cache);
651649
}
652-
#if FEATURE_COMPONENT_COLLECTION
653650
else
654651
{
655-
IContainer cont = component.Site.Container;
652+
IContainer? cont = component.Site.Container;
656653
if (cont != null)
657654
{
658655
return GetExtenders(cont.Components, instance, cache);
659656
}
660657
}
661-
#endif
662658
}
663659
return Array.Empty<IExtenderProvider>();
664660
}

src/libraries/System.ComponentModel.TypeConverter/tests/ComponentResourceManagerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private class TestSite : ISite
167167
public bool DesignMode { get; set; }
168168

169169
public IComponent Component => throw new NotImplementedException();
170-
public IContainer Container => throw new NotImplementedException();
170+
public IContainer Container => null;
171171
public string Name { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
172172
public object GetService(Type serviceType) => null;
173173
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.ComponentModel.Design;
6+
using System.Linq;
7+
using System.Runtime.InteropServices;
8+
using Xunit;
9+
10+
namespace System.ComponentModel.Tests
11+
{
12+
public class ReflectTypeDescriptionProviderTests
13+
{
14+
[Fact]
15+
public void GetAttributes_Skips_ComVisibleAttribute_And_GuidAttribute_And_InterfaceTypeAttribute()
16+
{
17+
AttributeCollection attributeCollection = TypeDescriptor.GetAttributes(typeof(TestClass1));
18+
Assert.NotEmpty(attributeCollection);
19+
IEnumerable<Attribute> attributes = attributeCollection.Cast<Attribute>();
20+
Attribute attribute = Assert.Single(attributes);
21+
Assert.IsType<DescriptionAttribute>(attribute);
22+
Assert.DoesNotContain(attributes, attr => attr.GetType() == typeof(ComVisibleAttribute));
23+
Assert.DoesNotContain(attributes, attr => attr.GetType() == typeof(GuidAttribute));
24+
Assert.DoesNotContain(attributes, attr => attr.GetType() == typeof(InterfaceTypeAttribute));
25+
}
26+
27+
[Fact]
28+
public void GetExtenderProviders_ReturnResultFromContainerComponents_WhenComponentSiteServiceNull()
29+
{
30+
using ComponentExtendedProvider testComponent = new ComponentExtendedProvider();
31+
testComponent.Site = new TestSiteWithoutService();
32+
testComponent.Disposed += (object obj, EventArgs args) => { };
33+
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(testComponent);
34+
PropertyDescriptor testPropDescriptor = propertyDescriptorCollection["TestProp"];
35+
Assert.NotNull(testPropDescriptor);
36+
ExtenderProvidedPropertyAttribute extenderProvidedPropertyAttribute = testPropDescriptor.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute;
37+
Assert.NotNull(extenderProvidedPropertyAttribute);
38+
Assert.IsType<ComponentExtendedProvider>(extenderProvidedPropertyAttribute.Provider);
39+
}
40+
41+
[Fact]
42+
public void GetExtenderProviders_ReturnResultFromComponentSiteService_WhenComponentSiteServiceNotNull()
43+
{
44+
using TestComponent testComponent = new TestComponent();
45+
testComponent.Site = new TestSiteWithService();
46+
testComponent.Disposed += (object obj, EventArgs args) => { };
47+
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(testComponent);
48+
PropertyDescriptor testPropDescriptor = propertyDescriptorCollection["TestProp"];
49+
Assert.NotNull(testPropDescriptor);
50+
ExtenderProvidedPropertyAttribute extenderProvidedPropertyAttribute = testPropDescriptor.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute;
51+
Assert.NotNull(extenderProvidedPropertyAttribute);
52+
Assert.IsType<TestExtenderProvider>(extenderProvidedPropertyAttribute.Provider);
53+
}
54+
55+
[ComVisible(true), Guid("4a223ebb-fe95-4649-94e5-2e5cc8f5f4e9"), InterfaceType(1)]
56+
internal interface TestInterface
57+
{
58+
void Action();
59+
}
60+
61+
[Description]
62+
internal class TestClass1 : TestInterface
63+
{
64+
public void Action() { }
65+
}
66+
67+
internal class TestComponent : IComponent
68+
{
69+
public ISite? Site { get; set; }
70+
71+
public event EventHandler? Disposed;
72+
73+
public void Dispose()
74+
{
75+
if (Disposed != null)
76+
{
77+
Disposed(this, new EventArgs());
78+
}
79+
}
80+
}
81+
82+
[ProvideProperty("TestProp", "System.Object")]
83+
internal class ComponentExtendedProvider : IComponent, IExtenderProvider
84+
{
85+
private int _testProp;
86+
87+
public ISite? Site { get; set; }
88+
89+
public event EventHandler? Disposed;
90+
91+
public bool CanExtend(object extendee) => true;
92+
93+
public int GetTestProp(object extendee) => _testProp;
94+
95+
public void SetTestProp(object extendee, int value)
96+
{
97+
_testProp = value;
98+
}
99+
100+
public void Dispose()
101+
{
102+
if (Disposed != null)
103+
{
104+
Disposed(this, new EventArgs());
105+
}
106+
}
107+
}
108+
109+
internal class TestSiteWithoutService : ISite
110+
{
111+
private TestComponent _testComponent = new TestComponent();
112+
private TestContainer _testContainer = new TestContainer();
113+
114+
public IComponent Component => _testComponent;
115+
116+
public IContainer? Container => _testContainer;
117+
118+
public bool DesignMode => true;
119+
120+
public string? Name { get; set; }
121+
122+
public object? GetService(Type serviceType) => null;
123+
}
124+
125+
internal class TestSiteWithService : ISite
126+
{
127+
private TestComponent _testComponent = new TestComponent();
128+
private TestContainer _testContainer = new TestContainer();
129+
private TestExtenderListService _testExtenderListService = new TestExtenderListService();
130+
131+
public IComponent Component => _testComponent;
132+
133+
public IContainer? Container => _testContainer;
134+
135+
public bool DesignMode => true;
136+
137+
public string? Name { get; set; }
138+
139+
public object? GetService(Type serviceType) => _testExtenderListService;
140+
}
141+
142+
internal class TestContainer : IContainer
143+
{
144+
private List<IComponent> _components = new List<IComponent> { new ComponentExtendedProvider() };
145+
146+
public ComponentCollection Components => new ComponentCollection(_components.ToArray());
147+
148+
public void Add(IComponent? component) => _components.Add(component);
149+
public void Add(IComponent? component, string? name) => _components.Add(component);
150+
public void Dispose() => _components.Clear();
151+
public void Remove(IComponent? component) => _components.Remove(component);
152+
}
153+
154+
[ProvideProperty("TestProp", "System.Object")]
155+
internal class TestExtenderProvider : IExtenderProvider
156+
{
157+
private int _testProp;
158+
159+
public bool CanExtend(object extendee) => true;
160+
161+
public int GetTestProp(object extendee) => _testProp;
162+
163+
public void SetTestProp(object extendee, int value)
164+
{
165+
_testProp = value;
166+
}
167+
}
168+
169+
internal class TestExtenderListService : IExtenderListService
170+
{
171+
private TestExtenderProvider _testExtenderProvider = new TestExtenderProvider();
172+
173+
public IExtenderProvider[] GetExtenderProviders() => new[] { _testExtenderProvider };
174+
}
175+
}
176+
}

src/libraries/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Compile Include="NestedContainerTests.cs" />
2222
<Compile Include="PasswordPropertyTextAttributeTests.cs" />
2323
<Compile Include="MarshalByValueComponentTests.cs" />
24+
<Compile Include="ReflectTypeDescriptionProviderTests.cs" />
2425
<Compile Include="ToolboxItemAttributeTests.cs" />
2526
<Compile Include="ToolboxItemFilterAttributeTests.cs" />
2627
<Compile Include="WarningExceptionTests.cs" />

src/libraries/System.Data.Common/tests/TrimmingTests/DbConnectionStringBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class TestSite : INestedSite
136136
{
137137
public string FullName => null;
138138
public IComponent Component => throw new NotImplementedException();
139-
public IContainer Container => throw new NotImplementedException();
139+
public IContainer Container => null;
140140
public bool DesignMode => throw new NotImplementedException();
141141
public string Name { get => "Test Component Name"; set => throw new NotImplementedException(); }
142142
public object GetService(Type serviceType) => null;

0 commit comments

Comments
 (0)