本文整理汇总了C#中ObjectConstructor类的典型用法代码示例。如果您正苦于以下问题:C# ObjectConstructor类的具体用法?C# ObjectConstructor怎么用?C# ObjectConstructor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ObjectConstructor类属于命名空间,在下文中一共展示了ObjectConstructor类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateObjectUsingCreatorWithParameters
private object CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor<object> creator, string id)
{
ValidationUtils.ArgumentNotNull(creator, "creator");
// only need to keep a track of properies presence if they are required or a value should be defaulted if missing
Dictionary<JsonProperty, PropertyPresence> propertiesPresence = (contract.HasRequiredOrDefaultValueProperties || HasFlag(Serializer._defaultValueHandling, DefaultValueHandling.Populate))
? contract.Properties.ToDictionary(m => m, m => PropertyPresence.None)
: null;
Type objectType = contract.UnderlyingType;
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
{
string parameters = string.Join(", ", contract.CreatorParameters.Select(p => p.PropertyName).ToArray());
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using creator with parameters: {1}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType, parameters)), null);
}
IDictionary<string, object> extensionData;
IDictionary<JsonProperty, object> propertyValues = ResolvePropertyAndCreatorValues(contract, containerProperty, reader, objectType, out extensionData);
object[] creatorParameterValues = new object[contract.CreatorParameters.Count];
IDictionary<JsonProperty, object> remainingPropertyValues = new Dictionary<JsonProperty, object>();
foreach (KeyValuePair<JsonProperty, object> propertyValue in propertyValues)
{
JsonProperty property = propertyValue.Key;
JsonProperty matchingCreatorParameter;
if (contract.CreatorParameters.Contains(property))
{
matchingCreatorParameter = property;
}
else
{
// check to see if a parameter with the same name as the underlying property name exists and match to that
matchingCreatorParameter = contract.CreatorParameters.ForgivingCaseSensitiveFind(p => p.PropertyName, property.UnderlyingName);
}
if (matchingCreatorParameter != null)
{
int i = contract.CreatorParameters.IndexOf(matchingCreatorParameter);
creatorParameterValues[i] = propertyValue.Value;
}
else
{
remainingPropertyValues.Add(propertyValue);
}
if (propertiesPresence != null)
{
// map from creator property to normal property
JsonProperty presenceProperty = propertiesPresence.Keys.FirstOrDefault(p => p.PropertyName == property.PropertyName);
if (presenceProperty != null)
propertiesPresence[presenceProperty] = (propertyValue.Value == null) ? PropertyPresence.Null : PropertyPresence.Value;
}
}
object createdObject = creator(creatorParameterValues);
if (id != null)
AddReference(reader, id, createdObject);
OnDeserializing(reader, contract, createdObject);
// go through unused values and set the newly created object's properties
foreach (KeyValuePair<JsonProperty, object> remainingPropertyValue in remainingPropertyValues)
{
JsonProperty property = remainingPropertyValue.Key;
object value = remainingPropertyValue.Value;
if (ShouldSetPropertyValue(property, value))
{
property.ValueProvider.SetValue(createdObject, value);
}
else if (!property.Writable && value != null)
{
// handle readonly collection/dictionary properties
JsonContract propertyContract = Serializer._contractResolver.ResolveContract(property.PropertyType);
if (propertyContract.ContractType == JsonContractType.Array)
{
JsonArrayContract propertyArrayContract = (JsonArrayContract)propertyContract;
object createdObjectCollection = property.ValueProvider.GetValue(createdObject);
if (createdObjectCollection != null)
{
IWrappedCollection createdObjectCollectionWrapper = propertyArrayContract.CreateWrapper(createdObjectCollection);
IWrappedCollection newValues = propertyArrayContract.CreateWrapper(value);
foreach (object newValue in newValues)
{
createdObjectCollectionWrapper.Add(newValue);
}
}
}
else if (propertyContract.ContractType == JsonContractType.Dictionary)
{
JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)propertyContract;
object createdObjectDictionary = property.ValueProvider.GetValue(createdObject);
//.........这里部分代码省略.........
示例2: JsonArrayContract
//.........这里部分代码省略.........
}
#if !(NET20 || NET35)
if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ISet<>)))
{
CreatedType = typeof(HashSet<>).MakeGenericType(CollectionItemType);
}
#endif
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
canDeserialize = true;
ShouldCreateWrapper = true;
}
#if !(NET40 || NET35 || NET20 || PORTABLE40)
else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyCollection<>), out tempCollectionType))
{
CollectionItemType = tempCollectionType.GetGenericArguments()[0];
if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyCollection<>))
|| ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyList<>)))
{
CreatedType = typeof(ReadOnlyCollection<>).MakeGenericType(CollectionItemType);
}
_genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType);
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, CollectionItemType);
IsReadOnlyOrFixedSize = true;
canDeserialize = HasParameterizedCreatorInternal;
}
#endif
else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable<>), out tempCollectionType))
{
CollectionItemType = tempCollectionType.GetGenericArguments()[0];
if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable<>)))
{
CreatedType = typeof(List<>).MakeGenericType(CollectionItemType);
}
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
#if !(NET35 || NET20)
if (!HasParameterizedCreatorInternal && underlyingType.Name == FSharpUtils.FSharpListTypeName)
{
FSharpUtils.EnsureInitialized(underlyingType.Assembly());
_parameterizedCreator = FSharpUtils.CreateSeq(CollectionItemType);
}
#endif
if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
_genericCollectionDefinitionType = tempCollectionType;
IsReadOnlyOrFixedSize = false;
ShouldCreateWrapper = false;
canDeserialize = true;
}
else
{
_genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType);
IsReadOnlyOrFixedSize = true;
ShouldCreateWrapper = true;
canDeserialize = HasParameterizedCreatorInternal;
}
}
else
{
// types that implement IEnumerable and nothing else
canDeserialize = false;
ShouldCreateWrapper = true;
}
CanDeserialize = canDeserialize;
#if (NET20 || NET35)
if (CollectionItemType != null && ReflectionUtils.IsNullableType(CollectionItemType))
{
// bug in .NET 2.0 & 3.5 that List<Nullable<T>> throws an error when adding null via IList.Add(object)
// wrapper will handle calling Add(T) instead
if (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(List<>), out tempCollectionType)
|| (IsArray && !IsMultidimensionalArray))
{
ShouldCreateWrapper = true;
}
}
#endif
#if !(NET20 || NET35 || NET40)
Type immutableCreatedType;
ObjectConstructor<object> immutableParameterizedCreator;
if (ImmutableCollectionsUtils.TryBuildImmutableForArrayContract(underlyingType, CollectionItemType, out immutableCreatedType, out immutableParameterizedCreator))
{
CreatedType = immutableCreatedType;
_parameterizedCreator = immutableParameterizedCreator;
IsReadOnlyOrFixedSize = true;
CanDeserialize = true;
}
#endif
}
示例3: JsonDictionaryContract
/// <summary>
/// Initializes a new instance of the <see cref="JsonDictionaryContract"/> class.
/// </summary>
/// <param name="underlyingType">The underlying type for the contract.</param>
public JsonDictionaryContract(Type underlyingType)
: base(underlyingType)
{
ContractType = JsonContractType.Dictionary;
Type keyType;
Type valueType;
if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary<,>), out _genericCollectionDefinitionType))
{
keyType = _genericCollectionDefinitionType.GetGenericArguments()[0];
valueType = _genericCollectionDefinitionType.GetGenericArguments()[1];
if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IDictionary<,>)))
CreatedType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType);
#if !(NET40 || NET35 || NET20 || PORTABLE40)
IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyDictionary<,>));
#endif
}
#if !(NET40 || NET35 || NET20 || PORTABLE40)
else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyDictionary<,>), out _genericCollectionDefinitionType))
{
keyType = _genericCollectionDefinitionType.GetGenericArguments()[0];
valueType = _genericCollectionDefinitionType.GetGenericArguments()[1];
if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IReadOnlyDictionary<,>)))
CreatedType = typeof(ReadOnlyDictionary<,>).MakeGenericType(keyType, valueType);
IsReadOnlyOrFixedSize = true;
}
#endif
else
{
ReflectionUtils.GetDictionaryKeyValueTypes(UnderlyingType, out keyType, out valueType);
if (UnderlyingType == typeof(IDictionary))
CreatedType = typeof(Dictionary<object, object>);
}
if (keyType != null && valueType != null)
{
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, typeof(KeyValuePair<,>).MakeGenericType(keyType, valueType));
#if !(NET35 || NET20)
if (!HasParameterizedCreatorInternal && underlyingType.Name == FSharpUtils.FSharpMapTypeName)
{
FSharpUtils.EnsureInitialized(underlyingType.Assembly());
_parameterizedCreator = FSharpUtils.CreateMap(keyType, valueType);
}
#endif
}
ShouldCreateWrapper = !typeof(IDictionary).IsAssignableFrom(CreatedType);
DictionaryKeyType = keyType;
DictionaryValueType = valueType;
#if (NET20 || NET35)
if (DictionaryValueType != null && ReflectionUtils.IsNullableType(DictionaryValueType))
{
Type tempDictioanryType;
// bug in .NET 2.0 & 3.5 that Dictionary<TKey, Nullable<TValue>> throws an error when adding null via IDictionary[key] = object
// wrapper will handle calling Add(T) instead
if (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(Dictionary<,>), out tempDictioanryType))
{
ShouldCreateWrapper = true;
}
}
#endif
#if !(NET20 || NET35 || NET40)
Type immutableCreatedType;
ObjectConstructor<object> immutableParameterizedCreator;
if (ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(underlyingType, DictionaryKeyType, DictionaryValueType, out immutableCreatedType, out immutableParameterizedCreator))
{
CreatedType = immutableCreatedType;
_parameterizedCreator = immutableParameterizedCreator;
IsReadOnlyOrFixedSize = true;
}
#endif
}
示例4: CreateObjectUsingCreatorWithParameters
private object CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor<object> creator, string id)
{
ValidationUtils.ArgumentNotNull(creator, nameof(creator));
// only need to keep a track of properies presence if they are required or a value should be defaulted if missing
bool trackPresence = (contract.HasRequiredOrDefaultValueProperties || HasFlag(Serializer._defaultValueHandling, DefaultValueHandling.Populate));
Type objectType = contract.UnderlyingType;
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
{
string parameters = string.Join(", ", contract.CreatorParameters.Select(p => p.PropertyName).ToArray());
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using creator with parameters: {1}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType, parameters)), null);
}
List<CreatorPropertyContext> propertyContexts = ResolvePropertyAndCreatorValues(contract, containerProperty, reader, objectType);
if (trackPresence)
{
foreach (JsonProperty property in contract.Properties)
{
if (propertyContexts.All(p => p.Property != property))
{
propertyContexts.Add(new CreatorPropertyContext
{
Property = property,
Name = property.PropertyName,
Presence = PropertyPresence.None
});
}
}
}
object[] creatorParameterValues = new object[contract.CreatorParameters.Count];
foreach (CreatorPropertyContext context in propertyContexts)
{
// set presence of read values
if (trackPresence)
{
if (context.Property != null && context.Presence == null)
{
object v = context.Value;
PropertyPresence propertyPresence;
if (v == null)
{
propertyPresence = PropertyPresence.Null;
}
else if (v is string)
{
propertyPresence = CoerceEmptyStringToNull(context.Property.PropertyType, context.Property.PropertyContract, (string)v)
? PropertyPresence.Null
: PropertyPresence.Value;
}
else
{
propertyPresence = PropertyPresence.Value;
}
context.Presence = propertyPresence;
}
}
JsonProperty constructorProperty = context.ConstructorProperty;
if (constructorProperty == null && context.Property != null)
{
constructorProperty = contract.CreatorParameters.ForgivingCaseSensitiveFind(p => p.PropertyName, context.Property.UnderlyingName);
}
if (constructorProperty != null && !constructorProperty.Ignored)
{
// handle giving default values to creator parameters
// this needs to happen before the call to creator
if (trackPresence)
{
if (context.Presence == PropertyPresence.None || context.Presence == PropertyPresence.Null)
{
if (constructorProperty.PropertyContract == null)
{
constructorProperty.PropertyContract = GetContractSafe(constructorProperty.PropertyType);
}
if (HasFlag(constructorProperty.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate))
{
context.Value = EnsureType(
reader,
constructorProperty.GetResolvedDefaultValue(),
CultureInfo.InvariantCulture,
constructorProperty.PropertyContract,
constructorProperty.PropertyType);
}
}
}
int i = contract.CreatorParameters.IndexOf(constructorProperty);
creatorParameterValues[i] = context.Value;
context.Used = true;
}
}
//.........这里部分代码省略.........
示例5: TryBuildImmutableForDictionaryContract
internal static bool TryBuildImmutableForDictionaryContract(Type underlyingType, Type keyItemType, Type valueItemType, out Type createdType, out ObjectConstructor<object> parameterizedCreator)
{
if (underlyingType.IsGenericType())
{
Type underlyingTypeDefinition = underlyingType.GetGenericTypeDefinition();
string name = underlyingTypeDefinition.FullName;
ImmutableCollectionTypeInfo definition = DictionaryContractImmutableCollectionDefinitions.FirstOrDefault(d => d.ContractTypeName == name);
if (definition != null)
{
Type createdTypeDefinition = underlyingTypeDefinition.Assembly().GetType(definition.CreatedTypeName);
Type builderTypeDefinition = underlyingTypeDefinition.Assembly().GetType(definition.BuilderTypeName);
if (createdTypeDefinition != null && builderTypeDefinition != null)
{
MethodInfo mb = builderTypeDefinition.GetMethods().FirstOrDefault(m =>
{
ParameterInfo[] parameters = m.GetParameters();
return m.Name == "CreateRange" && parameters.Length == 1 && parameters[0].ParameterType.IsGenericType() && parameters[0].ParameterType.GetGenericTypeDefinition() == typeof(IEnumerable<>);
});
if (mb != null)
{
createdType = createdTypeDefinition.MakeGenericType(keyItemType, valueItemType);
MethodInfo method = mb.MakeGenericMethod(keyItemType, valueItemType);
parameterizedCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParametrizedConstructor(method);
return true;
}
}
}
}
createdType = null;
parameterizedCreator = null;
return false;
}
示例6: TryBuildImmutableForArrayContract
internal static bool TryBuildImmutableForArrayContract(Type underlyingType, Type collectionItemType, out Type createdType, out ObjectConstructor<object> parameterizedCreator)
{
if (underlyingType.IsGenericType())
{
string name = underlyingType.GetGenericTypeDefinition().FullName;
ImmutableCollectionTypeInfo definition = ArrayContractImmutableCollectionDefinitions.FirstOrDefault(d => d.ContractTypeName == name);
if (definition != null)
{
Type createdTypeDefinition = Type.GetType(definition.CreatedTypeName + ", System.Collections.Immutable");
Type builderTypeDefinition = Type.GetType(definition.BuilderTypeName + ", System.Collections.Immutable");
if (createdTypeDefinition != null && builderTypeDefinition != null)
{
MethodInfo mb = builderTypeDefinition.GetMethods().FirstOrDefault(m => m.Name == "CreateRange" && m.GetParameters().Length == 1);
if (mb != null)
{
createdType = createdTypeDefinition.MakeGenericType(collectionItemType);
MethodInfo method = mb.MakeGenericMethod(collectionItemType);
parameterizedCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParametrizedConstructor(method);
return true;
}
}
}
}
createdType = null;
parameterizedCreator = null;
return false;
}
示例7: CreateObjectUsingCreatorWithParameters
// Token: 0x06000BD7 RID: 3031
// RVA: 0x00045760 File Offset: 0x00043960
private object CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor<object> creator, string id)
{
ValidationUtils.ArgumentNotNull(creator, "creator");
Dictionary<JsonProperty, JsonSerializerInternalReader.PropertyPresence> arg_70_0;
if (!contract.HasRequiredOrDefaultValueProperties && !this.HasFlag(this.Serializer._defaultValueHandling, DefaultValueHandling.Populate))
{
arg_70_0 = null;
}
else
{
arg_70_0 = Enumerable.ToDictionary<JsonProperty, JsonProperty, JsonSerializerInternalReader.PropertyPresence>(contract.Properties, (JsonProperty m) => m, (JsonProperty m) => JsonSerializerInternalReader.PropertyPresence.None);
}
Dictionary<JsonProperty, JsonSerializerInternalReader.PropertyPresence> dictionary = arg_70_0;
Type underlyingType = contract.UnderlyingType;
if (this.TraceWriter != null && this.TraceWriter.LevelFilter >= TraceLevel.Info)
{
string arg = string.Join(", ", Enumerable.ToArray<string>(Enumerable.Select<JsonProperty, string>(contract.CreatorParameters, (JsonProperty p) => p.PropertyName)));
this.TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, StringUtils.FormatWith("Deserializing {0} using creator with parameters: {1}.", CultureInfo.InvariantCulture, contract.UnderlyingType, arg)), null);
}
IDictionary<string, object> dictionary3;
IDictionary<JsonProperty, object> dictionary2 = this.ResolvePropertyAndCreatorValues(contract, containerProperty, reader, underlyingType, out dictionary3);
object[] array = new object[contract.CreatorParameters.Count];
IDictionary<JsonProperty, object> dictionary4 = new Dictionary<JsonProperty, object>();
foreach (KeyValuePair<JsonProperty, object> current in dictionary2)
{
JsonProperty property = current.Key;
JsonProperty jsonProperty;
if (contract.CreatorParameters.Contains(property))
{
jsonProperty = property;
}
else
{
jsonProperty = StringUtils.ForgivingCaseSensitiveFind<JsonProperty>(contract.CreatorParameters, (JsonProperty p) => p.PropertyName, property.UnderlyingName);
}
if (jsonProperty != null)
{
int num = contract.CreatorParameters.IndexOf(jsonProperty);
array[num] = current.Value;
}
else
{
dictionary4.Add(current);
}
if (dictionary != null)
{
JsonProperty jsonProperty2 = Enumerable.FirstOrDefault<JsonProperty>(dictionary.Keys, (JsonProperty p) => p.PropertyName == property.PropertyName);
if (jsonProperty2 != null)
{
dictionary[jsonProperty2] = ((current.Value == null) ? JsonSerializerInternalReader.PropertyPresence.Null : JsonSerializerInternalReader.PropertyPresence.Value);
}
}
}
object obj = creator(array);
if (id != null)
{
this.AddReference(reader, id, obj);
}
this.OnDeserializing(reader, contract, obj);
foreach (KeyValuePair<JsonProperty, object> current2 in dictionary4)
{
JsonProperty key = current2.Key;
object value = current2.Value;
if (this.ShouldSetPropertyValue(key, value))
{
key.ValueProvider.SetValue(obj, value);
}
else if (!key.Writable && value != null)
{
JsonContract jsonContract = this.Serializer._contractResolver.ResolveContract(key.PropertyType);
if (jsonContract.ContractType == JsonContractType.Array)
{
JsonArrayContract jsonArrayContract = (JsonArrayContract)jsonContract;
object value2 = key.ValueProvider.GetValue(obj);
if (value2 == null)
{
continue;
}
IWrappedCollection wrappedCollection = jsonArrayContract.CreateWrapper(value2);
IWrappedCollection wrappedCollection2 = jsonArrayContract.CreateWrapper(value);
IEnumerator enumerator3 = wrappedCollection2.GetEnumerator();
try
{
while (enumerator3.MoveNext())
{
object current3 = enumerator3.Current;
wrappedCollection.Add(current3);
}
continue;
}
finally
{
IDisposable disposable = enumerator3 as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
//.........这里部分代码省略.........
示例8: CreateWrapper
internal IWrappedCollection CreateWrapper(object list)
{
if (_genericWrapperCreator == null)
{
_genericWrapperType = typeof(CollectionWrapper<>).MakeGenericType(CollectionItemType);
Type constructorArgument;
if (ReflectionUtils.InheritsGenericDefinition(_genericCollectionDefinitionType, typeof(List<>))
|| _genericCollectionDefinitionType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
constructorArgument = typeof(ICollection<>).MakeGenericType(CollectionItemType);
}
else
{
constructorArgument = _genericCollectionDefinitionType;
}
ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { constructorArgument });
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(genericWrapperConstructor);
}
return (IWrappedCollection)_genericWrapperCreator(list);
}
示例9: CreateWrapper
internal IWrappedDictionary CreateWrapper(object dictionary)
{
if (_genericWrapperCreator == null)
{
_genericWrapperType = typeof(DictionaryWrapper<,>).MakeGenericType(DictionaryKeyType, DictionaryValueType);
ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { _genericCollectionDefinitionType });
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(genericWrapperConstructor);
}
return (IWrappedDictionary)_genericWrapperCreator(dictionary);
}
示例10: JsonArrayContract
/// <summary>
/// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
/// </summary>
/// <param name="underlyingType">The underlying type for the contract.</param>
public JsonArrayContract(Type underlyingType)
: base(underlyingType)
{
ContractType = JsonContractType.Array;
IsArray = CreatedType.IsArray;
bool canDeserialize;
Type tempCollectionType;
if (IsArray)
{
CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType);
IsReadOnlyOrFixedSize = true;
_genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType);
canDeserialize = true;
IsMultidimensionalArray = (IsArray && UnderlyingType.GetArrayRank() > 1);
}
else if (typeof(IList).IsAssignableFrom(underlyingType))
{
if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType))
CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
else
CollectionItemType = ReflectionUtils.GetCollectionItemType(underlyingType);
if (underlyingType == typeof(IList))
CreatedType = typeof(List<object>);
if (CollectionItemType != null)
_parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection<>));
canDeserialize = true;
}
else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType))
{
CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ICollection<>))
|| ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IList<>)))
CreatedType = typeof(List<>).MakeGenericType(CollectionItemType);
if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ISet<>)))
CreatedType = typeof(HashSet<>).MakeGenericType(CollectionItemType);
_parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
canDeserialize = true;
ShouldCreateWrapper = true;
}
else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable<>), out tempCollectionType))
{
CollectionItemType = tempCollectionType.GetGenericArguments()[0];
if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable<>)))
CreatedType = typeof(List<>).MakeGenericType(CollectionItemType);
_parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
#if !(NET35 || NET20 || NETFX_CORE)
if (!HasParametrizedCreator && underlyingType.Name == FSharpUtils.FSharpListTypeName)
{
FSharpUtils.EnsureInitialized(underlyingType.Assembly());
_parametrizedCreator = FSharpUtils.CreateSeq(CollectionItemType);
}
#endif
if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
_genericCollectionDefinitionType = tempCollectionType;
IsReadOnlyOrFixedSize = false;
ShouldCreateWrapper = false;
canDeserialize = true;
}
else
{
_genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType);
IsReadOnlyOrFixedSize = true;
ShouldCreateWrapper = true;
canDeserialize = HasParametrizedCreator;
}
}
else
{
// types that implement IEnumerable and nothing else
canDeserialize = false;
ShouldCreateWrapper = true;
}
CanDeserialize = canDeserialize;
}
示例11: JsonDictionaryContract
/// <summary>
/// Initializes a new instance of the <see cref="JsonDictionaryContract"/> class.
/// </summary>
/// <param name="underlyingType">The underlying type for the contract.</param>
public JsonDictionaryContract(Type underlyingType)
: base(underlyingType)
{
ContractType = JsonContractType.Dictionary;
Type keyType;
Type valueType;
if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary<,>), out _genericCollectionDefinitionType))
{
keyType = _genericCollectionDefinitionType.GetGenericArguments()[0];
valueType = _genericCollectionDefinitionType.GetGenericArguments()[1];
if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IDictionary<,>)))
CreatedType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType);
}
else
{
ReflectionUtils.GetDictionaryKeyValueTypes(UnderlyingType, out keyType, out valueType);
if (UnderlyingType == typeof(IDictionary))
CreatedType = typeof(Dictionary<object, object>);
}
if (keyType != null && valueType != null)
{
_parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, typeof(KeyValuePair<,>).MakeGenericType(keyType, valueType));
if (!HasParametrizedCreator && underlyingType.Name == FSharpUtils.FSharpMapTypeName)
{
FSharpUtils.EnsureInitialized(underlyingType.Assembly());
_parametrizedCreator = FSharpUtils.CreateMap(keyType, valueType);
}
}
ShouldCreateWrapper = !typeof(IDictionary).IsAssignableFrom(CreatedType);
DictionaryKeyType = keyType;
DictionaryValueType = valueType;
}
示例12: CreateWrapper
// Token: 0x06000A94 RID: 2708
// RVA: 0x0004156C File Offset: 0x0003F76C
internal IWrappedCollection CreateWrapper(object list)
{
if (this._genericWrapperCreator == null)
{
this._genericWrapperType = typeof(CollectionWrapper<>).MakeGenericType(new Type[]
{
this.CollectionItemType
});
Type type;
if (!ReflectionUtils.InheritsGenericDefinition(this._genericCollectionDefinitionType, typeof(List<>)))
{
if (this._genericCollectionDefinitionType.GetGenericTypeDefinition() != typeof(IEnumerable<>))
{
type = this._genericCollectionDefinitionType;
goto IL_8B;
}
}
type = typeof(ICollection<>).MakeGenericType(new Type[]
{
this.CollectionItemType
});
IL_8B:
ConstructorInfo constructor = this._genericWrapperType.GetConstructor(new Type[]
{
type
});
this._genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParametrizedConstructor(constructor);
}
return (IWrappedCollection)this._genericWrapperCreator(new object[]
{
list
});
}