Skip to content

Commit

Permalink
fix(30024): Removing needless loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksim Golev committed Aug 4, 2023
1 parent 5f80bd5 commit 1f20655
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,22 +293,17 @@ private static void CheckDefaultProvider(Type type)
contextForAdd = new ResetEventContext(resetEventForAdd, Environment.CurrentManagedThreadId);
s_processedTypes.Add(type, contextForAdd);

// Always use core reflection when checking for
// the default provider attribute. If there is a
// provider, we probably don't want to build up our
// own cache state against the type. There shouldn't be
// more than one of these, but walk anyway. Walk in
// reverse order so that the most derived takes precidence.
var attrs = type.GetCustomAttributes<TypeDescriptionProviderAttribute>(false)
.ToArray();
var providerAttr = type.GetCustomAttributes<TypeDescriptionProviderAttribute>(false)
.SingleOrDefault();
bool providerAdded = false;
for (int i = 0; i != attrs.Length; i++)

if (providerAttr != null)
{
Type? providerType = Type.GetType(attrs[i].TypeName);
Type? providerType = Type.GetType(providerAttr.TypeName);
if (providerType != null && typeof(TypeDescriptionProvider).IsAssignableFrom(providerType))
{
TypeDescriptionProvider prov = (TypeDescriptionProvider)Activator.CreateInstance(providerType)!;
AddProvider(prov, type);
TypeDescriptionProvider provider = (TypeDescriptionProvider)Activator.CreateInstance(providerType)!;
AddProvider(provider, type);
providerAdded = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,18 +810,35 @@ public void IsSupportedType_NullTypeWithParent_ThrowsArgumentNullException()
AssertExtensions.Throws<ArgumentNullException>("type", () => provider.IsSupportedType(null));
}

[Fact]
public async void GetConverter_ByMultithread_ReturnsExpected()
public static IEnumerable<object[]> GetConverter_ByMultithread_ReturnsExpected_TestData()
{
yield return new object[] { typeof(MyClass), typeof(MyTypeConverter) };
yield return new object[] { typeof(MyInheritedClassWithCustomTypeDescriptionProvider), typeof(MyInheritedClassWithCustomTypeDescriptionProviderConverter) };
yield return new object[] { typeof(MyInheritedClassWithInheritedTypeDescriptionProvider), typeof(MyTypeConverter) };
}

[Theory]
[MemberData(nameof(GetConverter_ByMultithread_ReturnsExpected_TestData))]
public async void GetConverter_ByMultithread_ReturnsExpected(Type typeForGetConverter, Type expectedConverterType)
{
TypeConverter[] actualConverters = await Task.WhenAll(
Enumerable.Range(0, 100).Select(_ =>
Task.Run(() => TypeDescriptor.GetConverter(typeof(MyClass)))));
Task.Run(() => TypeDescriptor.GetConverter(typeForGetConverter))));
Assert.All(actualConverters,
currentConverter => Assert.IsType<MyTypeConverter>(currentConverter));
currentConverter => Assert.IsType(expectedConverterType, currentConverter));
}

[Fact]
public async void GetConverterWithAddProvider_ByMultithread_Success()
public static IEnumerable<object[]> GetConverterWithAddProvider_ByMultithread_Success_TestData()
{
foreach (object[] currentTestCase in GetConverter_ByMultithread_ReturnsExpected_TestData())
{
yield return currentTestCase;
}
}

[Theory]
[MemberData(nameof(GetConverterWithAddProvider_ByMultithread_Success_TestData))]
public async void GetConverterWithAddProvider_ByMultithread_Success(Type typeForGetConverter, Type expectedConverterType)
{
TypeConverter[] actualConverters = await Task.WhenAll(
Enumerable.Range(0, 200).Select(_ =>
Expand All @@ -830,17 +847,26 @@ public async void GetConverterWithAddProvider_ByMultithread_Success()
var mockProvider = new Mock<TypeDescriptionProvider>(MockBehavior.Strict);
var someInstance = new object();
TypeDescriptor.AddProvider(mockProvider.Object, someInstance);
return TypeDescriptor.GetConverter(typeof(MyClass));
return TypeDescriptor.GetConverter(typeForGetConverter);
})));
Assert.All(actualConverters,
currentConverter => Assert.IsType<MyTypeConverter>(currentConverter));
currentConverter => Assert.IsType(expectedConverterType, currentConverter));
}

[TypeDescriptionProvider(typeof(MyClassTypeDescriptionProvider))]
public class MyClass
{
}

[TypeDescriptionProvider(typeof(MyInheritedClassWithCustomTypeDescriptionProviderTypeDescriptionProvider))]
public class MyInheritedClassWithCustomTypeDescriptionProvider : MyClass
{
}

public class MyInheritedClassWithInheritedTypeDescriptionProvider : MyClass
{
}

public class MyClassTypeDescriptionProvider : TypeDescriptionProvider
{
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
Expand All @@ -849,6 +875,14 @@ public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object
}
}

public class MyInheritedClassWithCustomTypeDescriptionProviderTypeDescriptionProvider : TypeDescriptionProvider
{
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
return new MyInheritedClassWithCustomTypeDescriptionProviderTypeDescriptor();
}
}

public class MyClassTypeDescriptor : CustomTypeDescriptor
{
public override TypeConverter GetConverter()
Expand All @@ -857,10 +891,22 @@ public override TypeConverter GetConverter()
}
}

public class MyInheritedClassWithCustomTypeDescriptionProviderTypeDescriptor : CustomTypeDescriptor
{
public override TypeConverter GetConverter()
{
return new MyInheritedClassWithCustomTypeDescriptionProviderConverter();
}
}

public class MyTypeConverter : TypeConverter
{
}

public class MyInheritedClassWithCustomTypeDescriptionProviderConverter : TypeConverter
{
}

private class SubTypeDescriptionProvider : TypeDescriptionProvider
{
public SubTypeDescriptionProvider() : base()
Expand Down

0 comments on commit 1f20655

Please sign in to comment.