public override JsonContract ResolveContract(Type type) { var contract = base.ResolveContract(type); if (contract is JsonObjectContract && !type.IsValueType && !type.HasDefaultConstructor()) { var defaultCreator = contract.DefaultCreator; contract.DefaultCreator = () => { try { // Todo: Structs without default constructor will fail with this and that will then try using the defaultCreator in the catch return _container.Get(type); } catch { return defaultCreator(); } }; } return contract; }
public static Delegate Creator(Type t) { if (t == typeof(string)) return Expression.Lambda(Expression.Constant(string.Empty)).Compile(); if (t.HasDefaultConstructor()) return Expression.Lambda(Expression.New(t)).Compile(); return null; }
/// <summary> /// Creates an instance of the given type using the default constructor. /// </summary> /// <param name="type">The type to create.</param> /// <returns>An instance of the type.</returns> /// <exception cref="NoTypesFoundException"> /// If no implementing types of <paramref name="type"/> was found. /// </exception> /// <exception cref="MissingDefaultConstructorException"> /// If the found type does not have a default constructor. /// </exception> public object Create(Type type) { if (!type.IsImplementation()) { throw new NoTypesFoundException(type); } if (!type.HasDefaultConstructor()) { throw new MissingDefaultConstructorException(type); } return Activator.CreateInstance(type); }
public DataToObjects(Type type, Func<Type, MemberInfo, int> membersOrder = null) { if (!DataType.IsPrimitiveType(type) && !type.HasDefaultConstructor()) throw new NotSupportedException("No default constructor."); bool isSupported = DataTypeUtils.IsAllPrimitive(type); if (!isSupported) throw new NotSupportedException("Not all types are primitive."); Type = type; MembersOrder = membersOrder; LambdaFromObjects = CreateFromObjectsMethod(); fromObjects = LambdaFromObjects.Compile(); LambdaToObjects = CreateToObjectsMethod(); toObjects = LambdaToObjects.Compile(); }
static void ThrowIfCanCreateContainerDoesNotHaveDefaultConstructor(Type createContainerType) { if (!createContainerType.HasDefaultConstructor()) throw new MissingDefaultConstructorException(createContainerType); }
public bool IsInstantiatable(Type type, Type targetType) { return !type.IsAbstract && !type.IsGenericType && type.HasDefaultConstructor() && targetType.IsAssignableFrom(type); }
public void AllSettingsHaveDefaultCtor(Type settingType) { Assert.True(settingType.HasDefaultConstructor(), string.Format("{0} should have default ctor.", settingType.Name)); }
private static bool RegistryHasDefaultConstructor(Type type) { return type.HasDefaultConstructor(); }
private bool IsValidDestinationType(Type destinationType) { return (destinationType.IsArray || destinationType.HasDefaultConstructor()); }