Skip to content

Commit 5a274b7

Browse files
committed
TypeDescriptor.AddAttribute has issues with some environments (even if Full Trust is enabled), therefore we register custom type converters and always call a util method instead of TypeDescriptor.GetConverter()
1 parent ac584bb commit 5a274b7

14 files changed

Lines changed: 71 additions & 75 deletions

File tree

src/Libraries/SmartStore.Core/Extensions/ConversionExtensions.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,36 @@
1515
using System.Security.Cryptography;
1616
using SmartStore.Core.ComponentModel;
1717
using SmartStore.Core.Domain.Shipping;
18+
using SmartStore.Core.Domain.Catalog;
1819

1920
namespace SmartStore
2021
{
2122

2223
public static class ConversionExtensions
2324
{
25+
private readonly static IDictionary<Type, TypeConverter> s_customTypeConverters;
26+
27+
static ConversionExtensions()
28+
{
29+
var intConverter = new GenericListTypeConverter<int>();
30+
var decConverter = new GenericListTypeConverter<decimal>();
31+
var stringConverter = new GenericListTypeConverter<string>();
32+
var soListConverter = new ShippingOptionListTypeConverter();
33+
var bundleDataListConverter = new ProductBundleDataListTypeConverter();
34+
35+
s_customTypeConverters = new Dictionary<Type, TypeConverter>();
36+
s_customTypeConverters.Add(typeof(List<int>), intConverter);
37+
s_customTypeConverters.Add(typeof(IList<int>), intConverter);
38+
s_customTypeConverters.Add(typeof(List<decimal>), decConverter);
39+
s_customTypeConverters.Add(typeof(IList<decimal>), decConverter);
40+
s_customTypeConverters.Add(typeof(List<string>), stringConverter);
41+
s_customTypeConverters.Add(typeof(IList<string>), stringConverter);
42+
s_customTypeConverters.Add(typeof(ShippingOption), new ShippingOptionTypeConverter());
43+
s_customTypeConverters.Add(typeof(List<ShippingOption>), soListConverter);
44+
s_customTypeConverters.Add(typeof(IList<ShippingOption>), soListConverter);
45+
s_customTypeConverters.Add(typeof(List<ProductBundleItemOrderData>), bundleDataListConverter);
46+
s_customTypeConverters.Add(typeof(IList<ProductBundleItemOrderData>), bundleDataListConverter);
47+
}
2448

2549
#region Object
2650

@@ -111,7 +135,7 @@ public static object Convert(this object value, Type to, CultureInfo culture)
111135
return new Guid((string)value);
112136

113137
// see if source or target types have a TypeConverter that converts between the two
114-
TypeConverter toConverter = TypeDescriptor.GetConverter(fromType);
138+
TypeConverter toConverter = GetTypeConverter(fromType);
115139

116140
Type nonNullableTo = to.GetNonNullableType();
117141
bool isNullableTo = to != nonNullableTo;
@@ -122,7 +146,7 @@ public static object Convert(this object value, Type to, CultureInfo culture)
122146
return isNullableTo ? Activator.CreateInstance(typeof(Nullable<>).MakeGenericType(nonNullableTo), result) : result;
123147
}
124148

125-
TypeConverter fromConverter = TypeDescriptor.GetConverter(nonNullableTo);
149+
TypeConverter fromConverter = GetTypeConverter(nonNullableTo);
126150

127151
if (fromConverter != null && fromConverter.CanConvertFrom(fromType))
128152
{
@@ -172,6 +196,16 @@ public static object Convert(this object value, Type to, CultureInfo culture)
172196
#endregion
173197
}
174198

199+
internal static TypeConverter GetTypeConverter(Type type)
200+
{
201+
TypeConverter converter;
202+
if (s_customTypeConverters.TryGetValue(type, out converter))
203+
{
204+
return converter;
205+
}
206+
return TypeDescriptor.GetConverter(type);
207+
}
208+
175209
#endregion
176210

177211
#region int

src/Libraries/SmartStore.Core/Extensions/XmlNodeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace SmartStore
66
{
7-
/// <remarks>codehint: sm-add</remarks>
7+
88
public static class XmlNodeExtensions
99
{
1010
/// <summary>Safe way to get inner text of an attribute.</summary>

src/Libraries/SmartStore.Core/Infrastructure/CommonStartupTask.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/Libraries/SmartStore.Core/Infrastructure/SmartStoreEngine.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ public void Initialize(SmartStoreConfig config)
8787
//startup tasks
8888
RunStartupTasks();
8989
}
90-
else
91-
{
92-
// TODO: (MC) not really good code. Find a better pattern!
93-
new CommonStartupTask().Execute();
94-
}
9590
}
9691

9792
public T Resolve<T>(string name = null) where T : class

src/Libraries/SmartStore.Core/SmartStore.Core.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@
212212
<Compile Include="Extensions\XmlWriterExtensions.cs" />
213213
<Compile Include="IMergedData.cs" />
214214
<Compile Include="Async\AsyncRunner.cs" />
215-
<Compile Include="Infrastructure\CommonStartupTask.cs" />
216215
<Compile Include="Infrastructure\DependencyManagement\AutofacLifetimeScopeProvider.cs" />
217216
<Compile Include="IO\Media\FileSystemStorageProvider.cs" />
218217
<Compile Include="IO\Media\IStorageFile.cs" />

src/Libraries/SmartStore.Core/Utilities/CommonHelper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.Dynamic;
45
using System.Globalization;
56
using System.IO;
@@ -134,5 +135,10 @@ public static ExpandoObject ToExpando(object value)
134135
return (ExpandoObject)expando;
135136
}
136137

138+
public static TypeConverter GetTypeConverter(Type type)
139+
{
140+
return ConversionExtensions.GetTypeConverter(type);
141+
}
142+
137143
}
138144
}

src/Libraries/SmartStore.Services/Configuration/SettingService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Reflection;
1515
using SmartStore.Core.Plugins;
1616
using System.ComponentModel;
17+
using SmartStore.Utilities;
1718

1819
namespace SmartStore.Services.Configuration
1920
{
@@ -339,7 +340,7 @@ public virtual bool SettingExists<T, TPropType>(T settings,
339340
continue;
340341
}
341342

342-
var converter = TypeDescriptor.GetConverter(prop.PropertyType);
343+
var converter = CommonHelper.GetTypeConverter(prop.PropertyType);
343344

344345
if (converter == null || !converter.CanConvertFrom(typeof(string)))
345346
continue;
@@ -369,7 +370,7 @@ public virtual void SetSetting<T>(string key, T value, int storeId = 0, bool cle
369370
Guard.ArgumentNotEmpty(() => key);
370371

371372
key = key.Trim().ToLowerInvariant();
372-
string valueStr = TypeDescriptor.GetConverter(typeof(T)).ConvertToInvariantString(value);
373+
string valueStr = CommonHelper.GetTypeConverter(typeof(T)).ConvertToInvariantString(value);
373374

374375
var allSettings = GetAllSettingsCached();
375376
var settingForCaching = allSettings.ContainsKey(key) ?
@@ -418,7 +419,7 @@ public virtual void SetSetting<T>(string key, T value, int storeId = 0, bool cle
418419
if (!prop.CanRead || !prop.CanWrite)
419420
continue;
420421

421-
if (!TypeDescriptor.GetConverter(prop.PropertyType).CanConvertFrom(typeof(string)))
422+
if (!CommonHelper.GetTypeConverter(prop.PropertyType).CanConvertFrom(typeof(string)))
422423
continue;
423424

424425
string key = typeof(T).Name + "." + prop.Name;

src/Libraries/SmartStore.Services/Filter/FilterService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using SmartStore.Core.Data;
1212
using System.Globalization;
1313
using SmartStore.Core;
14+
using SmartStore.Utilities;
1415

1516
namespace SmartStore.Services.Filter
1617
{
@@ -76,7 +77,7 @@ private object FilterValueToObject(string value, string type)
7677

7778
Type t = Type.GetType("System.{0}".FormatWith(ValidateValue(type, _defaultType)));
7879

79-
var result = TypeDescriptor.GetConverter(t).ConvertFromString(null, CultureInfo.InvariantCulture, value);
80+
var result = CommonHelper.GetTypeConverter(t).ConvertFromString(null, CultureInfo.InvariantCulture, value);
8081

8182
return result;
8283
}

src/Presentation/SmartStore.Web.Framework/Mvc/SmartModelBinder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Web;
66
using System.Web.Mvc;
77
using System.ComponentModel;
8+
using SmartStore.Utilities;
89

910
// codehint: sm-edit (massively: added proper dictionary binding)
1011

@@ -104,7 +105,7 @@ private object BindDictionary(ControllerContext controllerContext, ModelBindingC
104105

105106
private object ConvertType(string stringValue, Type type)
106107
{
107-
return TypeDescriptor.GetConverter(type).ConvertFrom(stringValue);
108+
return CommonHelper.GetTypeConverter(type).ConvertFrom(stringValue);
108109
}
109110

110111
private IEnumerable<string> GetValueProviderKeys(ControllerContext context)

src/Tests/SmartStore.Core.Tests/ComponentModel/ExpandoTests.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using SmartStore.Core.ComponentModel;
44
using SmartStore.Tests;
55
using NUnit.Framework;
6+
using SmartStore.Utilities;
67

78
namespace SmartStore.Core.Tests.ComponentModel
89
{
@@ -12,31 +13,27 @@ public class ExpandoTests
1213
[SetUp]
1314
public void SetUp()
1415
{
15-
TypeDescriptor.AddAttributes(typeof(List<int>),
16-
new TypeConverterAttribute(typeof(GenericListTypeConverter<int>)));
17-
TypeDescriptor.AddAttributes(typeof(List<string>),
18-
new TypeConverterAttribute(typeof(GenericListTypeConverter<string>)));
1916
}
2017

2118
[Test]
2219
public void Can_get_int_list_type_converter()
2320
{
24-
var converter = TypeDescriptor.GetConverter(typeof(List<int>));
21+
var converter = CommonHelper.GetTypeConverter(typeof(List<int>));
2522
converter.GetType().ShouldEqual(typeof(GenericListTypeConverter<int>));
2623
}
2724

2825
[Test]
2926
public void Can_get_string_list_type_converter()
3027
{
31-
var converter = TypeDescriptor.GetConverter(typeof(List<string>));
28+
var converter = CommonHelper.GetTypeConverter(typeof(List<string>));
3229
converter.GetType().ShouldEqual(typeof(GenericListTypeConverter<string>));
3330
}
3431

3532
[Test]
3633
public void Can_get_int_list_from_string()
3734
{
3835
var items = "10,20,30,40,50";
39-
var converter = TypeDescriptor.GetConverter(typeof(List<int>));
36+
var converter = CommonHelper.GetTypeConverter(typeof(List<int>));
4037
var result = converter.ConvertFrom(items) as IList<int>;
4138
result.ShouldNotBeNull();
4239
result.Count.ShouldEqual(5);
@@ -46,7 +43,7 @@ public void Can_get_int_list_from_string()
4643
public void Can_get_string_list_from_string()
4744
{
4845
var items = "foo, bar, day";
49-
var converter = TypeDescriptor.GetConverter(typeof(List<string>));
46+
var converter = CommonHelper.GetTypeConverter(typeof(List<string>));
5047
var result = converter.ConvertFrom(items) as List<string>;
5148
result.ShouldNotBeNull();
5249
result.Count.ShouldEqual(3);
@@ -56,7 +53,7 @@ public void Can_get_string_list_from_string()
5653
public void Can_convert_int_list_to_string()
5754
{
5855
var items = new List<int> { 10, 20, 30, 40, 50 };
59-
var converter = TypeDescriptor.GetConverter(items.GetType());
56+
var converter = CommonHelper.GetTypeConverter(items.GetType());
6057
var result = converter.ConvertTo(items, typeof(string)) as string;
6158

6259
result.ShouldNotBeNull();
@@ -67,7 +64,7 @@ public void Can_convert_int_list_to_string()
6764
public void Can_convert_string_list_to_string()
6865
{
6966
var items = new List<string> { "foo", "bar", "day" };
70-
var converter = TypeDescriptor.GetConverter(items.GetType());
67+
var converter = CommonHelper.GetTypeConverter(items.GetType());
7168
var result = converter.ConvertTo(items, typeof(string)) as string;
7269
result.ShouldNotBeNull();
7370
result.ShouldEqual("foo,bar,day");

0 commit comments

Comments
 (0)