Skip to content
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

Add trimming tests for System.ComponentModel.TypeConverter changes #38066

Merged
merged 3 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading;

Expand Down Expand Up @@ -325,7 +324,7 @@ private static void CheckDefaultProvider(Type type)
// 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.
object[] attrs = type.GetCustomAttributes(typeof(TypeDescriptionProviderAttribute), false).ToArray();
object[] attrs = type.GetCustomAttributes(typeof(TypeDescriptionProviderAttribute), false);
bool providerAdded = false;
for (int idx = attrs.Length - 1; idx >= 0; idx--)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
layomia marked this conversation as resolved.
Show resolved Hide resolved
using System.ComponentModel;

/// <summary>
/// Tests that the System.ComponentModel.TypeDescriptor.ComObjectType
/// property works as expected when used in a trimmed application.
/// </summary>
class Program
{
static int Main(string[] args)
{
Type type = TypeDescriptor.ComObjectType;

// Tests that the ctor for System.ComponentModel.TypeDescriptor+TypeDescriptorComObject is not trimmed out.
object obj = Activator.CreateInstance(type);
string expectedObjTypeNamePrefix = "System.ComponentModel.TypeDescriptor+TypeDescriptorComObject, System.ComponentModel.TypeConverter, Version=";

return obj != null && obj.GetType().AssemblyQualifiedName.StartsWith(expectedObjTypeNamePrefix)
? 100
: -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project DefaultTargets="Build">
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))" />

<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.targets))" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project DefaultTargets="Build">
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))" />

<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.targets))" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.ComponentModel;
using System.Xml.Linq;

class Program
{
/// <summary>
/// Tests that the public parameterless constructors of types passed to the
/// constructors of System.ComponentModel.TypeDescriptionProviderAttribute
/// property are preserved when needed in a trimmed application.
/// </summary>
static int Main(string[] args)
{
TypeDescriptionProviderAttribute attr = new TypeDescriptionProviderAttribute("Program+MyTypeDescriptionProvider");
if (!RunTest(attr))
{
return -1;
}

attr = new TypeDescriptionProviderAttribute(typeof(MyOtherTypeDescriptionProvider));
if (!RunTest(attr))
{
return -1;
}

return 100;
}

private static bool RunTest(TypeDescriptionProviderAttribute attr)
{
Type providerType = Type.GetType(attr.TypeName);

if (providerType != null && typeof(TypeDescriptionProvider).IsAssignableFrom(providerType))
{
TypeDescriptionProvider provider = (TypeDescriptionProvider)Activator.CreateInstance(providerType);
if (provider == null)
{
return false;
}
}
else
{
return false;
}

return true;
}

private class MyTypeDescriptionProvider : TypeDescriptionProvider { }

private class MyOtherTypeDescriptionProvider : TypeDescriptionProvider { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
layomia marked this conversation as resolved.
Show resolved Hide resolved
using System.ComponentModel;
using System.Globalization;

/// <summary>
/// Tests that System.ComponentModel.TypeConverter.ConvertFromInvariantString
/// is not trimmed out when needed by DefaultValueAttribute in a trimmed application.
/// </summary>
class Program
{
static int Main(string[] args)
{
TypeDescriptor.AddAttributes(typeof(string), new TypeConverterAttribute(typeof(MyStringConverter)));

var attribute = new DefaultValueAttribute(typeof(string), "Hello, world!");
return (string)attribute.Value == "Hello, world!trivia" ? 100 : -1;
}

private class MyStringConverter : StringConverter
{
/// <summary>
/// Converts the specified value object to a string object.
/// </summary>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
return (string)value + "trivia";
}

throw new NotSupportedException();
}
}
}