Skip to content

Commit a5ecf7c

Browse files
committed
Prevent conversion of the same type or interface
1 parent 88bb1ab commit a5ecf7c

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

src/ConvertEx/TypeConverter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public bool TryConvert(object value, Type targetType, IFormatProvider formatProv
6969
return true;
7070
}
7171

72+
// test same type
73+
var valueType = value.GetType();
74+
var implementingInterface = targetType.IsInterface && valueType.GetInterfaces().Contains(targetType);
75+
if (valueType == targetType || implementingInterface || valueType.IsSubclassOf(targetType))
76+
{
77+
convertedValue = value;
78+
return true;
79+
}
80+
7281
// get conversion path
7382
var path = GetCachedConversionPath(value.GetType(), targetType);
7483
if (path == null)
@@ -93,6 +102,7 @@ public bool TryConvert(object value, Type targetType, IFormatProvider formatProv
93102

94103
private IList<Type> GetCachedConversionPath(Type sourceType, Type destType)
95104
{
105+
// TODO implement GetCachedConversionPath properly
96106
return GetConversionPath(sourceType, destType);
97107
}
98108

tests/ConvertEx.Tests/TypeConverterTests.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,26 @@ public class TypeConverterTests
1111
[Theory]
1212
[InlineData(85, typeof(int), 85)]
1313
[InlineData("test", typeof(string), "test")]
14-
public void Test_SameType(object value, Type targetType, object expectedValue)
14+
[InlineData("test", typeof(object), "test")]
15+
public void Test_NoConversion(object value, Type targetType, object expectedValue)
1516
{
1617
TestConversion(value, targetType, expectedValue);
1718
}
1819

20+
[Fact]
21+
public void Test_NoSubClassConversion()
22+
{
23+
var b = new B();
24+
TestConversion(b, typeof(A), b);
25+
}
26+
27+
[Fact]
28+
public void Test_NoImplementedInterfaceConversion()
29+
{
30+
var a = new A();
31+
TestConversion(a, typeof(ITest), a);
32+
}
33+
1934
[Theory]
2035
[InlineData(85, typeof(double), 85d)]
2136
[InlineData(85, typeof(string), "85")]
@@ -29,13 +44,13 @@ public void Test_SimpleConversion(object value, Type targetType, object expected
2944
[Fact]
3045
public void Test_ValueToNullable()
3146
{
32-
TestConversion(100, typeof(int?), (int?) 100);
47+
TestConversion(100, typeof(int?), (int?)100);
3348
}
3449

3550
[Fact]
3651
public void Test_NullableToValue()
3752
{
38-
TestConversion((int?) 100, typeof(int), 100);
53+
TestConversion((int?)100, typeof(int), 100);
3954
}
4055

4156
[Fact]
@@ -59,5 +74,16 @@ private static void TestConversion(object value, Type targetType, object expecte
5974
var result = DotNetTools.ConvertEx.ConvertEx.ChangeType(value, targetType);
6075
Assert.Equal(expectedValue, result);
6176
}
77+
78+
private class A : ITest
79+
{
80+
81+
}
82+
83+
private class B : A
84+
{
85+
}
86+
87+
private interface ITest { }
6288
}
6389
}

0 commit comments

Comments
 (0)