Skip to content
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 @@ -101,7 +101,7 @@ public AssemblyName[] DependentAssemblies
}
set
{
Properties["DependentAssemblies"] = value.Clone();
Properties["DependentAssemblies"] = value?.Clone();
}
}

Expand Down Expand Up @@ -277,23 +277,15 @@ public string TypeName
}

/// <summary>
/// Gets the version for this toolboxitem. It defaults to AssemblyName.Version unless
/// overridden in a derived toolboxitem. This can be overridden to return an empty string
/// to suppress its display in the toolbox tooltip.
/// Gets the version for this toolboxitem. It defaults to AssemblyName.Version unless
/// overridden in a derived toolboxitem. This can be overridden to
/// return an empty string to suppress its display in the toolbox tooltip.
/// </summary>
public virtual string Version
{
get
{
if (AssemblyName != null)
{
return AssemblyName.Version.ToString();
}
return string.Empty;
}
get => AssemblyName?.Version?.ToString() ?? string.Empty;
}


/// <summary>
/// Occurs when components are created.
/// </summary>
Expand Down Expand Up @@ -408,7 +400,7 @@ protected virtual IComponent[] CreateComponentsCore(IDesignerHost host, IDiction
{
IComponent[] components = CreateComponentsCore(host);

if (host != null)
if (host != null && components != null)
{
for (int i = 0; i < components.Length; i++)
{
Expand Down Expand Up @@ -527,10 +519,9 @@ private static bool AreAssemblyNamesEqual(AssemblyName name1, AssemblyName name2

public override int GetHashCode()
{
string typeName = TypeName;
int hash = (typeName != null) ? typeName.GetHashCode() : 0;

return unchecked(hash ^ DisplayName.GetHashCode());
int typeHash = TypeName?.GetHashCode() ?? 0;
int displayHash = DisplayName?.GetHashCode() ?? 0;
return unchecked(typeHash ^ displayHash);
}

/// <summary>
Expand All @@ -542,8 +533,8 @@ protected virtual object FilterPropertyValue(string propertyName, object value)
switch (propertyName)
{
case "AssemblyName":
if (value != null)
value = ((AssemblyName)value).Clone();
if (value is AssemblyName valueName)
value = valueName.Clone();

break;

Expand Down Expand Up @@ -598,7 +589,7 @@ protected virtual Type GetType(IDesignerHost host, AssemblyName assemblyName, st

if (host != null)
{
ts = (ITypeResolutionService)host.GetService(typeof(ITypeResolutionService));
ts = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
}

if (ts != null)
Expand Down Expand Up @@ -664,7 +655,7 @@ protected virtual Type GetType(IDesignerHost host, AssemblyName assemblyName, st
{
}

if (a == null && assemblyName.CodeBase != null && assemblyName.CodeBase.Length > 0)
if (a == null && !string.IsNullOrEmpty(assemblyName.CodeBase))
{
try
{
Expand Down Expand Up @@ -804,7 +795,8 @@ public virtual void Initialize(Type type)

private AssemblyName GetNonRetargetedAssemblyName(Type type, AssemblyName policiedAssemblyName)
{
if (type == null || policiedAssemblyName == null)
Debug.Assert(type != null);
if (policiedAssemblyName == null)
return null;

//if looking for myself, just return it. (not a reference)
Expand Down Expand Up @@ -833,17 +825,17 @@ private AssemblyName GetNonRetargetedAssemblyName(Type type, AssemblyName polici
// in assemblyname.
foreach (AssemblyName name in type.Assembly.GetReferencedAssemblies())
{
Assembly a = null;

try
{
a = Assembly.Load(name);
if (a != null && a.FullName == policiedAssemblyName.FullName)
Assembly a = Assembly.Load(name);
if (a.FullName == policiedAssemblyName.FullName)
{
return name;
}
}
catch
{
//ignore all exceptions and just fall through if it fails (it shouldn't, but who knows).
// Ignore all exceptions and just fall through if it fails (it shouldn't, but who knows).
}
}

Expand Down Expand Up @@ -900,10 +892,7 @@ protected virtual void OnComponentsCreating(ToolboxComponentsCreatingEventArgs a
_componentsCreatingEvent?.Invoke(this, args);
}

public override string ToString()
{
return DisplayName;
}
public override string ToString() => DisplayName ?? string.Empty;

/// <summary>
/// Called as a helper to ValidatePropertyValue to validate that an object
Expand Down Expand Up @@ -992,6 +981,10 @@ protected virtual object ValidatePropertyValue(string propertyName, object value
value = filter;
break;

case "DependentAssemblies":
ValidatePropertyType(propertyName, value, typeof(AssemblyName[]), true);
break;

case "IsTransient":
ValidatePropertyType(propertyName, value, typeof(bool), false);
break;
Expand All @@ -1002,7 +995,6 @@ protected virtual object ValidatePropertyValue(string propertyName, object value
[SuppressMessage("Microsoft.Usage", "CA2240:ImplementISerializableCorrectly")]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
//IntSecurity.UnmanagedCode.Demand();
Serialize(info, context);
}

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@

<ItemGroup>
<ProjectReference Include="..\..\src\System.Windows.Forms.Design.csproj" />
<Compile Include="..\..\..\Common\tests\CommonMemberDataAttribute.cs" Link="Common\CommonMemberDataAttribute.cs" />
<Compile Include="..\..\..\Common\tests\CommonTestHelper.cs" Link="Common\CommonTestHelper.cs" />
<ProjectReference Include="..\..\..\Common\tests\InternalUtilitiesForTests\InternalUtilitiesForTests.csproj" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\16x16.bmp">
<LogicalName>System.Drawing.Design.Tests.16x16.bmp</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\24x16.bmp">
<LogicalName>System.Drawing.Design.Tests.24x16.bmp</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\16x24.bmp">
<LogicalName>System.Drawing.Design.Tests.16x24.bmp</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.Drawing.Design.Tests
{
public class ToolboxItemCollectionTests
{
[Fact]
public void ToolboxItemCollection_Ctor_ToolboxItemArray()
{
var item = new ToolboxItem();
var collection = new ToolboxItemCollection(new ToolboxItem[] { item });
Assert.Same(item, Assert.Single(collection));
Assert.Same(item, collection[0]);
Assert.True(collection.Contains(item));
Assert.Equal(0, collection.IndexOf(item));
}

[Fact]
public void ToolboxItemCollection_Ctor_NullToolboxItemArray_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>("c", () => new ToolboxItemCollection((ToolboxItem[])null));
}

[Fact]
public void ToolboxItemCollection_Ctor_ToolboxItemCollection()
{
var item = new ToolboxItem();
var value = new ToolboxItemCollection(new ToolboxItem[] { item });
var collection = new ToolboxItemCollection(value);
Assert.Same(item, Assert.Single(collection));
Assert.Same(item, collection[0]);
Assert.True(collection.Contains(item));
Assert.Equal(0, collection.IndexOf(item));
}

[Fact]
public void ToolboxItemCollection_Ctor_NullToolboxItemCollection_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>("c", () => new ToolboxItemCollection((ToolboxItemCollection)null));
}

[Fact]
public void ToolboxItemCollection_Contains_NoSuchValue_ReturnsFalse()
{
var item = new ToolboxItem();
var collection = new ToolboxItemCollection(new ToolboxItem[] { item });
Assert.False(collection.Contains(new ToolboxItem { DisplayName = "Other" }));
Assert.False(collection.Contains(null));
}

[Fact]
public void ToolboxItemCollection_IndexOf_NoSuchValue_ReturnsNegativeOne()
{
var item = new ToolboxItem();
var collection = new ToolboxItemCollection(new ToolboxItem[] { item });
Assert.Equal(-1, collection.IndexOf(new ToolboxItem { DisplayName = "Other" }));
Assert.Equal(-1, collection.IndexOf(null));
}

[Fact]
public void ToolboxItemCollection_CopyTo_Invoke_Success()
{
var item = new ToolboxItem();
var collection = new ToolboxItemCollection(new ToolboxItem[] { item });

var array = new ToolboxItem[3];
collection.CopyTo(array, 1);
Assert.Equal(new ToolboxItem[] { null, item, null }, array);
}
}
}
Loading